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?
init
hook happens before WP has fetched posts, but shortcodes only run when they're rendered, so the order is all wrong