0

I have a shortcode that I want to place below title of all my posts. Is there a hook or a filter I can use for this?

2
  • in the theme files, you can customise how to display post developer.wordpress.org/themes/template-files-section/… – Kaperto Dec 26 '19 at 23:03
  • you should customize your theme templates. That kind of filter hook is not available. Adding code before the 'the_content' won't be a good solution as the title and the content are two different piece of information ( they are typically, displayed in different locations in a template). So, your best choice would be to manually edit theme files (don't forget to use child theme). – maverick Dec 27 '19 at 10:55
1

Not really, but perhaps you using the wrong terms: A shortcode is a manually entered piece of text where code has been programmatically added so that when wp runs 'the_content' filter , it'll then call the function and replace the piece of text with the returned result of the function. It's a specialised form of a filter.

It sounds like what you want is that you'll need to know the actual function to call to get the text you want, ie the function that the shortcode calls, then write a filter that uses that function where the filter runs on the piece of html that you want to modify:

1) a filter on the title: There is an example here on using the 'the_title' filter https://developer.wordpress.org/plugins/hooks/filters/ or

2) Or on 'the_content', where you could call the function that creates the text you wand, add it to the front of the content, and return the result? https://developer.wordpress.org/reference/hooks/the_content/

Lots more filters here: https://codex.wordpress.org/Plugin_API/Filter_Reference

The above advice if implemented in a small plugin will work across any themes. If you just want it to work on one theme, you could create child theme (to avoid losing your changes if there is an update to the parent theme). In the child theme, you'd just modify the part of the template (maybe the header? or the post?) where you want to insert text.

1
  • +1 but I'd definitely go for the theme change rather than a filter. – Rup Dec 27 '19 at 1:49

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.