Completely resolved HTML for a given Post ID

Hey,

I’m in contact with a developer of a third party plugin who is trying to make a plugin compatible with X theme.

He wants me to ask the following:

How to get the final and completely resolved HTML for a given Post ID? The reason is that I need to analyze that HTML to extract all the images URLs in use.

Cheers.

Hi there,

Are you referring to a post’s featured image? If so, you can still the Wordpress get_the_post_thumbnail() function.

https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/

If you are referring to a different image, kindly let us know so that we could provide another suggestion.

Thank you.

Hi Jade,

We need, through PHP, to get the final HTML output of the post with all the shortcodes resolved. Getting the post_content and running the do_shortcodes function seems not enough (though normally it is for standard shortcodes).

Cheers

Hello Frederik,

Using X or even the default Twenty Nineteen theme, you can only get the post contents by using the get_post() function. You may find this links useful:

Regretfully, this particular customization request is outside the scope of our support as this is not related to an issue with the theme and instead has to do with your customization of it. As such, you will need to investigate this particular issue on your own or seek help from a developer should you not feel comfortable making these changes yourself. If you have any further questions about the theme, we are more than happy to provide you with assistance on these inquiries.

Thank you for your understanding.

Hey,

We know how to get the content of the post, that it is not the problem. The problem is that this content contains shortcodes. Those shortcodes are specific to X-Theme. To resolve shortcodes, the function do_shortcodes() is used. However, in the case of X-Theme, the shortcodes aren’t resolved. Knowing why would be interesting, but at least, we would simply like to know how to resolve those shortcodes. If that question is too technical or complicated: how can we get the content of a post with X-Theme with all the shortcodes resolved? That is to say, with the final HTML?

Best regards
Frederik

Hi Frederik,

Since V2 elements prevents access from outside then it will result to empty HTML once do_shortcode() is called, like if you have a custom query and call the V2 shortcodes. The workaround will be similar to this

//Let's says $post is the current post from your custom query

$content = $post->post_content;
$id = $post->ID;

$final_html = do_shortcode (str_replace("[cs_content]", "[cs_content _p=\"{$id}\" no_wrap=true ]" , $content ));

Then, the HTML you’ll looking is in the $final_html variable.

Thanks!

Hey Rad, Thanks for this!

I have another question:

How can I detect that x-theme is actually installed and enabled on my install? Typically, I would like to test if a class is present, like this: class_exists( ‘x_theme_class’ ). What class would be safe to test?

Best regards
Frederik

Hi Frederik,

You can try with this condition: if( class_exist('X_Bootstrap') ){.

Hope it helps :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.