Title: Call a wp function from a custom link
Last modified: January 23, 2025

---

# Call a wp function from a custom link

 *  [girl_number_5](https://wordpress.org/support/users/trutherone/)
 * (@trutherone)
 * [1 year, 4 months ago](https://wordpress.org/support/topic/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)?

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [1 year, 4 months ago](https://wordpress.org/support/topic/call-a-wp-function-from-a-custom-link/#post-18260366)
 * You can add your own menu items to the toolbar using this hook: [https://developer.wordpress.org/reference/hooks/admin_bar_menu/](https://developer.wordpress.org/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.
 *  Thread Starter [girl_number_5](https://wordpress.org/support/users/trutherone/)
 * (@trutherone)
 * [1 year, 4 months ago](https://wordpress.org/support/topic/call-a-wp-function-from-a-custom-link/#post-18260842)
 * Hi [@threadi](https://wordpress.org/support/users/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 :
 *     ```wp-block-code
       'title'  => $local_time,
       ```
   
 * `$local_time` key/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_time` a custom function call, i.e. `$split_to_columns(
   4)` and in this function we use the WP native function to do what we want.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [1 year, 4 months ago](https://wordpress.org/support/topic/call-a-wp-function-from-a-custom-link/#post-18261745)
 * $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?
 *  Thread Starter [girl_number_5](https://wordpress.org/support/users/trutherone/)
 * (@trutherone)
 * [1 year, 4 months ago](https://wordpress.org/support/topic/call-a-wp-function-from-a-custom-link/#post-18262372)
 * [@bcworkz](https://wordpress.org/support/users/bcworkz/)
 * 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.
 *     ```wp-block-code
           // 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.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [1 year, 4 months ago](https://wordpress.org/support/topic/call-a-wp-function-from-a-custom-link/#post-18263903)
 * 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](https://developer.wordpress.org/plugins/javascript/ajax/).
   The request should go through /wp-admin/admin-ajax.php, passing a particular `
   action` query string parameter. Your Ajax handler function would hook into an
   action whose name is in part based upon the `action` parameter’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.
 *  Thread Starter [girl_number_5](https://wordpress.org/support/users/trutherone/)
 * (@trutherone)
 * [1 year, 4 months ago](https://wordpress.org/support/topic/call-a-wp-function-from-a-custom-link/#post-18263930)
 * Hi [@bcworkz](https://wordpress.org/support/users/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??
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [1 year, 4 months ago](https://wordpress.org/support/topic/call-a-wp-function-from-a-custom-link/#post-18265139)
 * > 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

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Call a wp function from a custom link’ is closed to new replies.

## Tags

 * [WP-Toolbar](https://wordpress.org/support/topic-tag/wp-toolbar/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 10 replies
 * 3 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [1 year, 4 months ago](https://wordpress.org/support/topic/call-a-wp-function-from-a-custom-link/#post-18265139)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
