admin管理员组

文章数量:1024603

I'm using the snippet from Open Admin bar "Visit site" in a new window which works perfectly on the node I specify.

I need to adapt this to include all of the sub-nodes, which are dynamically created, but follow this logic:

node
 node_1
  node_1_view
  node_1_another
  node_1_andanother
 node_2
  node_2_view
  etc
 node_3
 etc

This gets additions following the same sequence.

How can I modify the code to set the target for all of these node?

I'm using the snippet from Open Admin bar "Visit site" in a new window which works perfectly on the node I specify.

I need to adapt this to include all of the sub-nodes, which are dynamically created, but follow this logic:

node
 node_1
  node_1_view
  node_1_another
  node_1_andanother
 node_2
  node_2_view
  etc
 node_3
 etc

This gets additions following the same sequence.

How can I modify the code to set the target for all of these node?

Share Improve this question asked Apr 15, 2019 at 23:49 Jarod ThorntonJarod Thornton 6384 silver badges18 bronze badges 1
  • What do you mean by nodes? Posts? Menu items? – norman.lol Commented Apr 16, 2019 at 7:46
Add a comment  | 

1 Answer 1

Reset to default 0

Perhaps you could use $wp_admin_bar->get_nodes(); to get all toolbar nodes, then loop them through and modify the right nodes as needed. Something along these lines,

add_action( 'admin_bar_menu', 'customize_my_wp_admin_bar', 80 );
function customize_my_wp_admin_bar( $wp_admin_bar ) {
  $all_toolbar_nodes = $wp_admin_bar->get_nodes();

  if ( ! $all_toolbar_nodes ) {
    return;
  }

  foreach ( $all_toolbar_nodes as $node ) {
    // Skip nodes you don't want to edit
    if ( ! $some_logic ) {
      continue;
    }
    //Change target
    $node->meta['target'] = '_blank';
    //Update Node.
    $wp_admin_bar->add_node($node);    
  }

}

I can't remember what properties $node has, but I guess there's some sort of parent property that you can use in the if statement to skip the wrong ones.

I'm using the snippet from Open Admin bar "Visit site" in a new window which works perfectly on the node I specify.

I need to adapt this to include all of the sub-nodes, which are dynamically created, but follow this logic:

node
 node_1
  node_1_view
  node_1_another
  node_1_andanother
 node_2
  node_2_view
  etc
 node_3
 etc

This gets additions following the same sequence.

How can I modify the code to set the target for all of these node?

I'm using the snippet from Open Admin bar "Visit site" in a new window which works perfectly on the node I specify.

I need to adapt this to include all of the sub-nodes, which are dynamically created, but follow this logic:

node
 node_1
  node_1_view
  node_1_another
  node_1_andanother
 node_2
  node_2_view
  etc
 node_3
 etc

This gets additions following the same sequence.

How can I modify the code to set the target for all of these node?

Share Improve this question asked Apr 15, 2019 at 23:49 Jarod ThorntonJarod Thornton 6384 silver badges18 bronze badges 1
  • What do you mean by nodes? Posts? Menu items? – norman.lol Commented Apr 16, 2019 at 7:46
Add a comment  | 

1 Answer 1

Reset to default 0

Perhaps you could use $wp_admin_bar->get_nodes(); to get all toolbar nodes, then loop them through and modify the right nodes as needed. Something along these lines,

add_action( 'admin_bar_menu', 'customize_my_wp_admin_bar', 80 );
function customize_my_wp_admin_bar( $wp_admin_bar ) {
  $all_toolbar_nodes = $wp_admin_bar->get_nodes();

  if ( ! $all_toolbar_nodes ) {
    return;
  }

  foreach ( $all_toolbar_nodes as $node ) {
    // Skip nodes you don't want to edit
    if ( ! $some_logic ) {
      continue;
    }
    //Change target
    $node->meta['target'] = '_blank';
    //Update Node.
    $wp_admin_bar->add_node($node);    
  }

}

I can't remember what properties $node has, but I guess there's some sort of parent property that you can use in the if statement to skip the wrong ones.

本文标签: Set anchor target for admin bar nodes