I want to tweak my existing theme's operation by using the_content filter.
I added this code into child theme's functions.php:
add_filter( 'the_content', 'va_added_custom_data', 99 );
function va_added_custom_data( $content ) {
// Check if we're inside the main loop in a single post page.
if ( is_single() && in_the_loop() && is_main_query() ) {
$html_segment_1 = '<p>111 This will be added to the top and the bottom of the current content.</p>';
$html_segment_2 = '<p>222 This will be added to the top and the bottom of the current content.</p>';
$content = $html_segment_1 . $content . $html_segment_2;
}
return $content;
}
It works properly on standard WP posts, but it shows duplications on Toolset CPTs!
Correct here ( pls add domain from related site field ):
/elso-1-bejegyzes-a-burger-king-rol/
Duplicated here:
/brand/aqua/
/brand-post/egy-tortenet-az-aqua-rol-es-a-burger-king-rol/
The same happens with default priority and also without the if condition.
Pls note
"Új bejegyzés" is "New Post" and it is a toolset content template inside another Toolset single Content template and it is wrapped unnecessarily with the content added by the filter shown above.
Any idea is appreciated,
Thank you!
Hi,
Thanks for asking! I'd be happy to help.
To troubleshoot and reproduce this on my test website, I'll need to see exactly how content templates of these posts are set up in the admin area.
Can you please share temporary admin login details in reply to this message?
Note: Your next reply will be private and though no changes will be made on your website, please make a complete backup copy, before sharing the access details.
regards,
Waqar
Hi,
it's been a while, so is there any development?
Thank you!
Hi,
Thank you for sharing these details.
During troubleshooting, I noticed that the custom content added through "the_content" filter is showing 3 times as the content/body is being processed 3 times.
Once from the theme's template file and then twice from the assigned content template "Template for Brand Posts".
( there is one "Fields and Text" block with post body shortcode and one "Content Template" block for the "Brand Post Form Template" template )
To avoid repetition and make sure that this custom content from the function is included only once, you can define a constant and include a condition to check if it is already defined:
( ref: hidden link )
Example:
add_filter( 'the_content', 'va_added_custom_data', 99 );
function va_added_custom_data( $content ) {
// Check if we're inside the main loop in a single post page.
if ( is_single() && in_the_loop() && is_main_query() ) {
$html_segment_1 = '<p>111 This will be added to the TOP of the current content.</p>';
$html_segment_2 = '<p>222 This will be added to the BOTTOM of the current content.</p>';
if (!defined('VA_CONTENT_ADDED'))
{
$content = $html_segment_1 . $content . $html_segment_2;
define('VA_CONTENT_ADDED', 'yes');
}
}
return $content;
}
I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar
Hi,
pls. hide my domain's link, I clearly indicated I don't want it to show for public.
I gonna get back to your reply later.
Thank you.
Hi,
The domain links are only visible to logged-in users, but I've removed that from my reply.
Please let me know if you have any follow-up question and for a new question or concern you're welcome to start a new ticket.
regards,
Waqar
Hi,
thank you for removing my link!
I modified according to your code, but there's still issues.
Your code adds my modification not in the right time ( pls. check all 3 links I referred to in my OP ),
therefore the added content is shown not on the right place, in case of Brand and Brand Post page.
Thank you!
If you can name me a hook after Toolset has modified the_content(), it might help.
Hi,
I'm afraid, I couldn't find any information about a hook that can be targeted after Toolset has modified the_content().
Since the_content() is very generic and is filtered by WordPress, themes, and plugins alike, for a very specific requirement such as yours it might not be a good idea to use it, in the first place. You'll continuously have to narrow down the scope of your custom function so that the output is only added when you actually want it.
Have you considered using custom hooks instead?
( ref: hidden link )
You'll find the the_content(); calls in your theme's template files and before and after that function you can add your custom hooks and insert your content to those hooks instead of "the_content".
regards,
Waqar
Hi,
I suppose adding a custom hook would cause the same issue as I have now, if I'm not able to exclude Toolset's the_content modification BEFORE the core WP sets the_content.
I've already installed a WP plugin ( Simply show hooks ) which shows the action and filter hooks on any frontend page, so if you are logged in as an admin and you are on a specific page like a Brand Post, on top admin bar you can enable showing those hooks.
A Toolset pro might find the correct hook I should have to hook on, after Tooset has done its modifications ( and only after that should set the php constant to "yes" to add my additional content ).
An ugly, hardcoded solution might be, when we set php constant to "yes" after the
3rd modification of the_content for Brand Posts CPT
and after the 2nd for Brands CPT ( numbers may vary )
( supposing the last one is the WP core modification )
Thank you
Hi,
> I suppose adding a custom hook would cause the same issue as I have now, if I'm not able to exclude Toolset's the_content modification BEFORE the core WP sets the_content.
- I would think differently since WordPress, theme, and plugins all hook/filter into the "the_content" whereas only your custom functions will be hooking into the custom hooks that you'll define.
If you'd like to avoid, the use of conditional checks for post type and the current order of execution, you'll need to avoid adding custom output through "the_content".
And even if you've narrowed down to a hook that fires after Toolset has done its modifications, you'll still need those conditional checks so that the output is not included where you don't want it.
The logic to place the tag of your custom hooks should be simple:
1. If you'd like the custom content to show before and after the output controlled by the theme's template, the before and after custom hook tags will need to be placed in the relevant template's file.
2. But if you'd like to show the custom content before or after the output controlled through the Toolset's content template ( e.g. Template for Brand Posts ), you can create a custom shortcode that returns the custom hooks tag and place that shortcode in the desired location in the content template.
I hope this makes sense.
regards,
Waqar
Hi,
I've already asked you not to include any reference to my website... 🙁
I have an optional solution which doesn't concatenate my content to the_content, but adds my content either before or after that, so there's no multiplication.
This is how Related Brand and Context added to a standard WP post ( available from frontpage, clicking any post with featured image of a hamburger ).
( the solution is what you described at:
https://toolset.com/forums/topic/extend-existing-themes-single-template-with-toolsets-entities-with-php-code/#post-1605131
using "toolset_get_related_posts" function and "wpv-post-taxonomy" shortcode, through "do_shortcode" function in PHP )
So all in all, I can solve my issue this way, but I still plan to test your custom hook idea with conditionals,
so pls. switch the status of the ticket to like Waiting For User Reply or whatever.
Thank you
Hi,
I couldn't find any mention of your website's domain, but if you were referring to those screenshots, they have been removed.
Please feel free to test out the custom hook approach and let me know how it goes.
For a new question/concern, please open a new ticket.
regards,
Waqar