Modifying $post->post_content in WordPress - wordpress

I'm using a template that seems to be outputting $post->post_content in the search page.
I maintain a plugin that uses a non-standard shortcode format and I'm trying to find out how can I filter $post->post_content before it gets displayed because currently my shortcode is not getting covered (again, not using the Shortcode API).
This has me stumped. Any help, I would appreciate.

I think you could use the_post action hook, that allows to modify the post object immediately after being queried and setup:
add_action('the_post', function($post, $query){
// do whatever you want to $post, for example:
$post->post_content = str_replace('{YOUR_SHORTCODE}', 'WHATEVER', $post->post_content);
}, 10, 2);

Related

How to determine the "post type" of a "page"

I've created a Wordpress template for a site and the site uses an events plugin. My template has a custom heading so in the header it displays different text according to the post-type.
I'm having difficulty with the plugin (The Events Calendar) however it isn't a plugin issue. I'm unable to determine if it is a "tribe_events" post type becuase the plugin generates a page so when I use the get_post_type() function, it returns that it is a page. How do you get the underlying post type of a page? Is it possible without having to write a direct MySQL or $wpdb query?
I know it's not the most efficient way to do this but I've resorted to using $wp_query->post and then using the available fields from the post.
global $wp_query;
$postObj = $wp_query->post;
Now I can use any of the available fields from $postObj
You can use global $post object to get post type.
global $post;
$post_type = $post->post_type;
var_dump($post); to check content of the object.

Customize search results for Custom Post Types

I am writing a Wordpress plug-in that creates several Custom Post Types (CPT). For they have their own custom fields, that need to be displayed in the search results, I need to customize the search results output.
Do I need to write my own theme for that or is there a hook (or other way) to solve this in my plug-in code?
You could hook into get_the_content and get_the_excerpt filters and test with is_search() to see if you should alter the returned value or not.
Not tested, but this is the idea:
add_filter( 'get_the_excerpt', 'my_search_excerpt' );
add_filter( 'get_the_content', 'my_search_excerpt' );
function my_search_excerpt( $content ) {
if ( is_search() ) {
$content = 'This is a search excerpt for ' . get_the_title();
// maybe add a read more link
// also, you can use global $post to access the current search result
}
return $content;
}
I see four possibilities:
Create a new (child) theme
Override the search template using filters (template_include)
Use client-side code to modify the appearance (CSS / JavaScript, poor workaround)
Hook the_content or the_excerpt
The easiest way might be to copy the search.php file of your installed theme and modify it to fulfill your needs. You can then hook it in using the first or second way. The first requires you to create a child theme, the second to create a plugin. The latter might be more complex so I would suggest to create a theme (take a look at template files of child themes for an explanation).

Create a blog post from content in another post type

The Wordpress site I'm working on has a section for "News" (which is the regular blog/posts) which will be used for any news the company has to write about. Then I have a custom post type for Promotions, which has it's own page.
I want the client to be able to add his promotion content through the custom post type, which is going on the Promotions page, however I'd like this content to also be "cross posted" into the blog/news without forcing the client to write it up twice.
Is there a way to do this? Thanks.
Just a note: The reason I have the promotions as a custom type on it's own instead of just having them do it all from the blog is because I needed custom fields that would be unnecessary for any other kind of blog post.
Two options:
1) Use the Shortcode API
And in your cross-post you'd add the shortcode [crosspost id="POST-ID"]. Where POST-ID corresponds to the numeric ID of the other post (post type). Instead of ID, the title could be used, see the function get_page_by_title.
Create your own plugin for this. Add a sample shortcode from the Codex and use the function get_post to get the contents of the cross-post.
2) Use Advanced Custom Fields plugin
With it, adding meta boxes with custom fields is a breeze. And it has a Post Object field that's basically a Cross Post functionality.
You could do it a lot more simply by adding a filter to wp_insert_data(). For example, in your theme's functions.php file add the following:
add_filter('wp_insert_post_data', 'post_to_other', 99, 2);
That filter will then run anytime you add a new post. In the function post_to_other(), you look to see what type of post is being submitted. If it's a promotion, then insert a second copy as a News item.
function post_to_other($post_id, $post){
/** check $post to see what type it is, if it's a promotion */
if($post->post_type == 'promotion'){
$second_post = array(
'post_type'=> 'post',
'post_title'=> $post->post_title,
'post_name' =>$post->post_name,
'post_content'=> $post->post_content,
'post_author'=> $post->post_author,
'post_status'=> 'publish',
'tax_input'=> array('taxonomy_name'=>array('news'))
);
wp_insert_post($second_post);
}
}
I'm running out the door so I don't have time to double check the exact code but that's the basic structure of it. The tax_input bit is optional, lets you specify a category if you want. You'll probably need to tweak it a bit but that's the basics.

How to map an arbitrary URL to a function in a Wordpress plugin

I'm trying to create a Wordpress plugin that redirects visitors of example.com/redirect/XXX to a different page based on the value of XXX. I think I know how to do the redirect logic, but I don't know how to make sure that my Wordpress plugin function will be called when a visitor goes to example.com/redirect. Right now I just get a 404. There are other solutions that involved changing the .htaccess file, but I want this to function as a standalone plugin. Thanks!
When I need this kind of things i just create a common page with template as the "plugin", a page with all the functions that I need.
For example, if i need a shopping cart, I just create a cart.php as:
<?php
/*
Template Name: Cart
*/
// functions here
?>
And I go to my wp-admin and create a page with Cart as its template.
Depending on what exactly you want to do which is quite vague ( when you say page you actually mean page ? post ? cpt ? and when you say plugin functions - what are they ? and do you use permalinks ?)
.. but under some conditions you could use wp conditionals .
example ( from codex )
is_singular( 'foo' )
// Returns true if the post_type is "foo". execute plugin hook
is_singular( array( 'foo', 'bar', 'baz' ) )
// Returns true if the post_type is "foo", "bar", or "baz".
// See also the Custom Post Types book.
or if you aim at filtering you can always hook to pre_get_post with is_main_query() or any other conditional //

Wordpress widget/plugin - content based on text of post(s)/page(s) visible?

I'm new to the Wordpress plugin and widget APIs, but I'm sure this is possible.
I'd like to make a plugin or widget that would create links to other posts or external sites based on certain keywords/tags in the content of the given page/post.
Is this possible?
For example, if a term is in all-caps, link to the Wiktionary definition; inside a <news>..</news> pair, go to Google's news search; etc.
This is definitely possible. Don't bother looking into the widget api for this. Look at the filter api. WordPress has an api that allows you to filter content before it's sent to the browser. In this case, you'd do something like this:
function my_super_awesome_content_filterer( $content ){
$content = preg_replace( '#([A-Z]+)#', '$1', $content );
}
add_filter( 'the_content', 'my_super_awesome_content_filterer' );
Read more about filters here:
http://codex.wordpress.org/Plugin_API#Filters

Resources