Utilizing my function as a shortcode and PHP function. (add_action/do_action help?)

New to WordPress development.

I have a function that returns an HTML table of usernames and IDs. I’ve successfully registered it as a shortcode with defaults.

I’m curious how to properly register it as a function that can be used in my other theme files. Would also like to see an example of passing arguments to `user_ID_list` elsewhere in the file. I’m guessing I need to utilize `add_action` and `do_action`, I’m but having trouble wrapping my head around how they work.

Trying to learn best practices on how to accomplish this.

Here’s the code – [https://gist.github.com/AakLak/26cc05d01f66af0c09dacab895a9c295](https://gist.github.com/AakLak/26cc05d01f66af0c09dacab895a9c295)

Thanks!

Edit:`if ( shortcode_exists( ‘list_users’ )) {echo ‘Hello?’;}`This doesn’t echo anything. However the shortcode works when used in pages.

Edit:

I solved it by changing:

`function user_list_shortcodes_init(){add_shortcode( ‘list_users’, ‘user_ID_list’ );}add_action(‘init’, ‘user_list_shortcodes_init’);`

To just the `add_shortcode` line. I’ll have to look into the quirks about this, I’m guessing this caused things to load in an incorrect order.

1 Comment
  1. You don’t need to do anything else. Just either use the shortcode in your content, or call it via do_shortcode.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer