0

I am trying to match tag names with form titles to fetch the correct form into the current post, to subscribe to new posts tagged with the current posts tag. I only have 1 tag assigned per post.

The code for categories works well, and I tried to transcribe it for tags, but I am afraid there are syntax errors in the code, because it does not work.

How do I write the correct code?

add_shortcode( 'subscribe-to-tag', function() {
   global $wpdb, $post;
 
$the_tag = get_the_tags( $post->ID );
$tag_name = $the_tag[0]->tag_name;
   $id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM wptq_forms WHERE name = '{$tag_name}';"));
   if (is_null($id)) { return ''; }
   return do_shortcode( '[newsletter_form id="' . intval( $id ) . '"]' );
} ); 
5
  • 1
    I did not test your code, but tag object as any term object doesn't have tag_name property, just name. Try to get a name like this $tag_name = $the_tag[0]->name;
    – anton
    May 10, 2021 at 22:28
  • What is the error message in the PHP error log? When you say it does not work, what does it do instead? How is the shortcode being used? and which plugin adds the wptq_forms table?
    – Tom J Nowell
    May 10, 2021 at 23:19
  • Hey Anton, that was it, now it works, thanks. Tom, it's the Mailster plugin. But I modify the code before posting because developers seem to want me to change prefixes and other theme or plugin specific information into something else. I am not sure why, and I also experienced that on StackOverflow and StackExchange I am supposed to ask generic questions and not refer to a certain concrete product like plugin or theme, I am not sure why either. Probably something about the business side of the code. People used to suggest to not say which plugin, or theme...anyone can help understand this? May 11, 2021 at 18:49
  • I did not test the code previosuly, so there was no error message, I said it doesn't work because I made up the code not from any reliable source, I didn't find anything relevant online. So it was not worth testing yet, without knowing the theory for sure. May 11, 2021 at 18:56
  • The shortcode is used within a page builder, it's dropped into my post template. May 11, 2021 at 18:58

1 Answer 1

1

I don't see PHP syntax errors (like unwanted brackets) in your code, but there are two WordPress-specific issues that need to be fixed:

  1. Note that wpdb::prepare() needs one or more placeholders (e.g. %s for strings and %d for numbers) and the replacement value for each placeholder.

    So in your case, the correct $wpdb->prepare() would be:

    $wpdb->prepare( "SELECT ID FROM wptq_forms WHERE name = %s", $tag_name );
    
  2. get_the_tags() returns an array of term objects on successful requests, and each object is a WP_Term instance which does not have a tag_name property, only name.

    Therefore $the_tag[0]->tag_name should instead be $the_tag[0]->name.

1
  • 1
    Thanks, it does work now with the tags, and I corrected the wpdb prepare section as well. May 11, 2021 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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