Wordpress method to parse existing posts - php

I have a WP blog with over 7500 posts and would like to run a "batch script" on all such posts, to apply some changes to each post's content. For example filter some words or reduce the number of characters/images etc.
I'm familiar with PHP, but I'm not really familiar with Wordpress and the WP Codex. I know what I need to code once I get access to the actual post content, but getting there and after that saving the new content to WP DB is what I don't know how to achieve.
What is the best (straightforward & easy) option - running some batch script through PHPMyAdmin or is there some WP plugin that can be modified to achieve arbitrary operations on posts' content?
Does anyone have such a batch script in their workspace, willing to share?

I finally found it by testing, and again "thanks dude" to the one who downvoted the question without answering, just because.
So, the way to parse WP posts (just for visitor output, without altering in wpdb) is to go to "/wp-includes/post-template.php" and alter the internals of the_content function, before the echo $content statement. That's the most straightforward, yet unsafe way (if you don't know what you're really doing). Another safer way is to actually use the apply_filter(the_content, 'your_filter') filter hook if you know your WP stuff.
That's it, happy PHP-ing!

Related

Dynamic content in PHP WordPress page

I have a ecommerce store website running with WordPress. I'd like to include a section with a -random custormer's product review, so that every time someone access the page, there will be a different comment there.
I'm not used to PHP, but I managed to create a shortcode which takes a random comment and returns the proper HTML. It is working fine (in eddition mode, every time I insert the shortcode a different comment appears).
My issue is that when I leave the page and return, the previous one is still there. I believe that it is being caused by cache, but I wouldn't like to disable the cache for the whole page.
How you I force the shortcode run again (I don't know if it is the right way to explain) and make sure that at every access, a different comment appears?
One solution I thought is to have JS code which would do preaty much the same thing my PHP code does, using Woocommerce API to get the data. But I'm wondering if there is a simpler solution to do that, like forcing the specific section not being cached or re-run the shortcode.
Thanks!
JS can't do what PHP does here: at most it can create an AJAX-call to the backend that then runs a query for a random comment and returns it. You need to render it thereafter. It's fairly standard, but overkill for your case.
Instead, you're going to want to check whether your caching mechanism supports ESI or something else that excluded parts of your code from being cached.

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
the shortcode is defined into a class of a plugin
the shortcode function sets some values into the object of the plugin
class
several public functions of the object called by the theme return status and parameters
memorized into the object
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
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

get latest posts from wordpress blog based on tags and display them in base site

this might be easy question for you but not for me as i am really new to wordpress, i have setup Wordpress in my site as blog(and i am quite happy with that),
i want to display some posts from my wordpress blog to my base site, i am looking around but its like headache as i have thousands of references and i am confused,
i want to display posts from certain tags,
any help would be much appreciated. Thanks in advance
Kind Regards
I use a program called rss2html setup with a category parameter. Costs a few bucks, but you can do practically anything with it (or via one of the companion programs available at same site). I have used it tons of times passing a category, custom query, or other parameter to it using the config file.
If you choose to pick up a copy and have questions configuring, gimme a shout, I've done literally hundreds of them.
You can find the program I use here: www.feedforall.com

Beginning to use wordpress, custom theme?

I'm pretty new to wordpress (only a couple of days), but I have it up and running on my website with the default theme. First of all I would like to change this theme. I have found a tutorial directly from wordpress but it is extremely vague: http://codex.wordpress.org/Theme_Development. I'd like to have a totally different layout for my website though, with only one page being replaced daily with a different article - a new article everyday basically. But I don't know where to start. I want to first create the theme though, I think that will put things in to perspective. How do I do so? I know the principles of php and enough about html and css to create whole websites. Thank you
That link should tell you everything you need to know. You basically just edit the theme files to get the layout that you want. The best way to learn truly is to experiment. If you have a specific question I could help you with that, but to tell everything involved in creating a custom theme would go way beyond the scope of an answer for this site.

PHP wordpress style related posts

Do you know how wordpress decides which related posts to show?I mean I know if it has tags, then it's easy but if it doesn't?
Best Regards,
Generally the 'related post' function of a Wordpress Blog is created by the owner of the blog, or using a plugin. I can imagine that those plugins either look for new posts with similar tags or in the same category as the post it is displaying.
As far as my knowledge extends, I do not believe that Wordpress has a show_related_posts function, although I may be wrong. If it does, it would be simple enough to look it up on the documentation at wordpress.org.
Also, another good part about OpenSource programming is that you can simply go in and look at any of the code that you are curious about. I personally use Notepad++, and when I find a function in Wordpress or any other CMS I am not famillar with, I simply copy and paste the function name, and do a Multi-File search on the function to figure out where it is declared.
I'm going to have to go with Chacha102, as far as I know this can only be done using plugins or by hacking the code yourself.
I did something similar by falling back on the post's category if it had no tags, works pretty well too.
This guy has the leading plugin for related posts. I'd download that and take a peek at his code and see how he does it. Then you can reverse engineer it and make it your own. The beauty of opensource.
http://wordpress.org/extend/plugins/wordpress-23-related-posts-plugin/

Categories