Call a wp function from a custom link
-
Hi,
Is it possible to call a built-in WP function from a sub-menu link of a custom wp-admin toolbar (the top horizontal admin bar, not the sidebar)?
-
You can add your own menu items to the toolbar using this hook: https://developer.ww.wp.xz.cn/reference/hooks/admin_bar_menu/
You also define the link target here, which you can then use to call up a WP function of your choice.
Hi @threadi
thanks, coincidentally, i saw that page yesterday before posting and should have made reference to it. am i right in thinking then that the :
'title' => $local_time,$local_timekey/val could be a call to the native WP function – most of the magic in wptoolbar seems to happen in the ‘title’ key. So without using hooks could we just make$local_timea custom function call, i.e.$split_to_columns(4)and in this function we use the WP native function to do what we want.$local_time is just a string value. You can pass any string you like under the key “title”. How you compose that string would be where any magic might happen. In the example I believe you’re looking at, this would be the magic part:
$local_time = date( 'Y-m-d, g:i a', current_time( 'timestamp', 0 ) );You need to use the “admin_bar_menu” hook in order to get the WP_Admin_Bar object on which you can add or remove nodes.
What are you actually trying to do now that we’ve established that WP functions can be called?
I’m trying to call the,
flush_rewrite_rules();wp function. I’ve already built the submenu and its functions ok. I can respond to clicks on the button in jQuery and show a message just to prove its working. So what do i hook into in WordPress to call the flush function. Is there such a hook as ‘wpbar_node_click’ or something?? During the call to this function i may pass an argument which allows user (admins only) to do a ‘hard’ or ‘soft’ flush.// first child node $args = array( 'id' => 'pinflsh', 'title' => '<button id="btn-flush-perms"><small>Flush Permalinks</small></button>', 'href' => '', 'parent' => 'pinmain', 'meta' => array( 'class' => 'pin-styles', 'title' => '' ) ); $wp_admin_bar->add_node($args);I’ve also experimented with calling a php file using ajax on-button click, the file contains the,
flush_rewrite_rules();function but chrome developer inspector says the function is not recognised.Using the “admin_bar_menu” hook from which to execute functions would execute any time the admin bar appears on any page. Since flush_rewrite_rules() is resource intensive, this is not what you should be doing. flush_rewrite_rules() should only be called on an intermittent, as needed basis.
Causing it to be called from a click event on a menu item is OK. Calling it every time the admin bar appears is not OK. It’s possible to use Ajax techniques to to cause the rules to be flushed. A click event would send an Ajax request to the server and the server side Ajax handler function can make the call.
The reason for the undefined function error you are getting is the WP environment was not properly initialized by the requested file. Using Ajax should alleviate that problem, but there’s a particular way to handle Ajax in WP. The request should go through /wp-admin/admin-ajax.php, passing a particular
actionquery string parameter. Your Ajax handler function would hook into an action whose name is in part based upon theactionparameter’s value.FWIW, you can cause the rules to be flushed by requesting the permalinks settings screen. No need to alter or change anything, simply loading the page is adequate.
Hi @bcworkz
Thanks for that – just to address your last point first; this menu is for admins only but its also a convenience thing yes its saves us a few clicks including clicking the save button on the Permalinks screen just to save the configuration that’s already set. I’m building lots of CPTs, Taxonomies and Roles & Capabilities and each time having to remember to flush the cache otherwise it all goes belly-up.
I’m using
wp_localize_script()to properly call our ajax php file so your advice is probably the missing thing here."/wp-admin/admin-ajax.php, passing a particular action query string parameter"Can you give me an idea or example of how to use that technique or is there a link to the docs i can read??
is there a link to the docs i can read??
Follow the link in my previous reply 😉 After reviewing that page, continue on into the next two sections. The section prior to the linked page about jQuery might be informative as well.
There are a number of pieces that all have to work together to have functional Ajax in WP. Don’t be surprised when you need to do some debugging before everything works correctly. Once it’s all working and you understand what’s going on, you’ll find it’s not really all that complex. But on the first pass through it’s a lot to take in. At least that was my experience when I first tried to do Ajax in WP. YMMV
The topic ‘Call a wp function from a custom link’ is closed to new replies.