Trebly
Trebly

Reputation: 31

Find a process to check if a shortcode is/was present in a post : Something wrong in my tries

My purpose is to set some treatment options of the theme which depends of the presence of some shortcodes (and their parameters).

Detailed purpose, reasons of the need for information

In this way the user can define theme options into the "page" or the "article" (I meet this because the site needs some pages and articles as text displayed in single column, like a book and a side bar, while others using multimedia need full width on a PC with several panels).

what fails : The shortcode is executed but I cannot get back into the theme that it has been and get the parameters for the execution of the theme.

The structure that I have implemented

Something wrong into this process

There is something wrong because when "page.php" is run the function included which asks to the object if the shortcode function has been run answers "not run".

This while it can be verified some line after (getting the post content) that the post displayed is with the shortcode treated.

Further when "sidebar" is executed the same question is answered "true".

May be

May be my concept is wrong because the process treatment of the post (shortcodes particularly) is done only when the content of the post is output by "the_content()" WP function (later and too late into the page.php) ?

If this is true I should have to analyse myself the content of the post before any treatment. Nothing seems to be told about this (or not found) into the Wordpress Codex.

Question

Do you have any idea. I have some ones for a solution but if the cause that I imagine is wrong I would have produced something too complicated.

Best regards

Trebly

Upvotes: 0

Views: 792

Answers (1)

Trebly
Trebly

Reputation: 31

Thanks for your informations. During the short time since I post this subject I have developed the solution.

I confirm that the problem is that when you write $pt = $post->get_content();

you get get the raw content before any shortcode treatment.

It is when you write into your theme -?php the_content() ?- that the shortcodes are treated and the result is buffered.

Then I read my $pt and search for the requested shortcode. If I find it, I analyse, get parameters and execute.

But this is not ended because you need to clear the shortcode or any other with a purpose of same type but not treated into your branch of code (the best is to not change the post_content). This is done by my process, the function called by the shortcode returning an empty string.

This is a mean to give instructions to the template about how to behave with the current content ( by the html content of pages, articles or others post lists).

Nevertheless it is not always possible for any content because many contents are introduced by shortcodes and you cannot generally give instructions at the second level. I say "generally" because you can develop shortcodes which will use a callback function launched before ob is sent. Then you can analyse the ob and make some changes. This is the theoretically impossible second level. It is not easy to manage encoding but it is the way that I uses to produce Tables of Contents (headers or others user tags) for any output of WP.

Best regards

Trebly

Upvotes: 0

Related Questions