0

I would like to collect all attributes of the shortcodes that are stored in a page in the ShortCodes and then output them all. I do these in a Plugin.

1.) Set a global Variable in init.php ==> $allShortcodeAtts = array();

2.) store atts in the global variable in the hook

function wpdocs_bartag_func( $atts ) {
 $atts = shortcode_atts( array(
    'foo' => 'no foo',
    'baz' => 'default baz'
 ), $atts, 'bartag' );
 global $allShortcodeAtts;
 $allShortcodeAtts[] = $atts;

 /* do other stuff */
 
 return "foo = {$atts['foo']}";
}

function register_shortcodes() {
   add_shortcode ( 'bartag', 'wpdocs_bartag_func' );
}
add_action ( 'init', 'register_shortcodes', 10 );

3.) Deposit shortcode in post_content => [bartag foo="bar1"] [bartag foo="bar2"]

4.) output all varaibles of the shortcodes

function after_register_shortcodes(){
 global $allShortcodeAtts;

 var_dump($allShortcodeAtts);
}
add_action ( 'init', 'after_register_shortcodes', 999 );

no attributes are stored in the global variables. I do not understand why?

5
  • The init hook happens before WP has fetched posts, but shortcodes only run when they're rendered, so the order is all wrong – Tom J Nowell Apr 27 at 18:38
  • i understand, thats wrong. So many thanks... – Severin Apr 27 at 18:40
  • It now works wonderfully in the frontend. In the backend unfortunately not yet. There I execute the shortcodes with do_shortcode(). Which hook can I use in the backend after do_shortcode()? – Severin Apr 27 at 19:51
  • What are you trying to do that requires this? It's quite an unusual thing to be doing, some context will help – Tom J Nowell Apr 27 at 19:57
  • Thank you Tom. With the shortcode I generate different tables with different columns. In the attributes of the shortcode are the columns that are displayed. So far I generate the data of the tables for each shortcode. To store the data in a query, I need all columns of all tables. Therefore the collecting of these data, for the times query of the data. – Severin Apr 27 at 20:38
1

Used shorcodes can be retrived only if they are added in post content (or any content saved in db). They can not be retrived if hardcoded in template files (TBH, they can, but that's not the issue here). This code can help you to get the shortcodes used in a post content.

function wpse387291_get_shortcodes() {
    global $post;
    
    $pattern = get_shortcode_regex();
    
    if (   preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches ) {
        //$matches will hold the shortcodes if any
        echo '<pre>' . print_r( $matches, true ) . '</pre>';
    }
}
add_action( 'wp', 'wpse387291_get_shortcodes' );

You can use any hook that runs after wp.

3
  • It now works wonderfully in the frontend. In the backend unfortunately not yet. There I execute the shortcodes with do_shortcode(). Which hook can I use in the backend after do_shortcode()? – Severin Apr 27 at 19:51
  • @Severin Using do_shortcode() mostly means you are printing it directly in HTML. There nothing (at least I know of) can be done. – Abhik Apr 28 at 17:28
  • I have tested many admin_* hooks for the backend from all wp hooks and none fire after do_shortcode(). I will have to look for another solution. Thank you for helping. – Severin Apr 28 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.