admin管理员组

文章数量:1200979

I have a template that adds a shortcode. The add_shortcode is at the top of the template, before any template output. Note that any attributes for the shortcode are optional. I don't care if they exist or not. I will just test for the attribute values and do something if they exist.

When are the shortcode attributes available for use in the template? Psuedocode example ($atts are the shortcode's attributes array):

// template header is here
// add the shortcode
add_shortcode("mycode", "myshortcodefunction");

// is $atts available here? Because they are need by the next 
//   included function
include('someotherfunction.php');  // edited to add this line


function myshortcodefunction() {
   echo "a shortcode processed here ";
   echo "and here are the attributes:<br>";
   print_r($atts);
   return;
  }

// is $atts available here?

// template code here
// show the page content in the loop
the_post();

// is $atts available here?

// end of template

// is $atts available here?

In which of those 'spots' in the template page code does $atts contain the shortcode parameters? And how do I use those parameters outside of the add_shortcode function?

Can I create a constant that contains $atts?

// ending template stuff

Added

The overall intent of my project is to use the attributes in an 'included' function that is loaded after the add_shortcode statement. That included function does a lot of work, including displaying text (a form), and using the shortcode attributes in other parts of that included function.

So, I need access to the shortcode attributes before the content (and it's shortcode) is processed.... in the first "is $atts available here" after the add_shortcode.

I've also changed the pseudocode above to show where that included function is placed - directly after the add_shortcode.

I have a template that adds a shortcode. The add_shortcode is at the top of the template, before any template output. Note that any attributes for the shortcode are optional. I don't care if they exist or not. I will just test for the attribute values and do something if they exist.

When are the shortcode attributes available for use in the template? Psuedocode example ($atts are the shortcode's attributes array):

// template header is here
// add the shortcode
add_shortcode("mycode", "myshortcodefunction");

// is $atts available here? Because they are need by the next 
//   included function
include('someotherfunction.php');  // edited to add this line


function myshortcodefunction() {
   echo "a shortcode processed here ";
   echo "and here are the attributes:<br>";
   print_r($atts);
   return;
  }

// is $atts available here?

// template code here
// show the page content in the loop
the_post();

// is $atts available here?

// end of template

// is $atts available here?

In which of those 'spots' in the template page code does $atts contain the shortcode parameters? And how do I use those parameters outside of the add_shortcode function?

Can I create a constant that contains $atts?

// ending template stuff

Added

The overall intent of my project is to use the attributes in an 'included' function that is loaded after the add_shortcode statement. That included function does a lot of work, including displaying text (a form), and using the shortcode attributes in other parts of that included function.

So, I need access to the shortcode attributes before the content (and it's shortcode) is processed.... in the first "is $atts available here" after the add_shortcode.

I've also changed the pseudocode above to show where that included function is placed - directly after the add_shortcode.

Share Improve this question edited Jun 11, 2022 at 3:36 Rick Hellewell asked Jun 10, 2022 at 20:35 Rick HellewellRick Hellewell 7,1062 gold badges22 silver badges41 bronze badges 2
  • Were you actually aware of function myshortcodefunction( $atts ), i.e. the $atts can and should be accessed like that?

    本文标签: When are Shortcode Attributes Available in Template