Automatically transform custom WordPress block - wordpress

I'm developing a custom Gutenberg block that will be used in posts that are converted to email-compatible HTML. The post-to-HTML converter only accepts a limited number of block types, so I'd like the block to automatically transform into a core/paragraph block when the post is saved (or another trigger).
I looked at the core Transforms API but was only able to use it for user-initiated transforms.
As a workaround, I'm using the PHP function below to filter the content when it's saved, but I'd like to find a more elegant solution using React.
add_filter( 'content_save_pre', 'filter_custom_content', 10, 1 );
function filter_custom_content( $value ) {
return str_replace( 'wp:custom-block/test-block -->', 'wp:core/paragraph --> ', $value );
}
Can the Transforms API be triggered to automatically transform a block? Or is there another solution that may work on the frontend?

Related

Single Post Template Override for Custom Post Type

I've got kind of a unique scenario that I'm trying to nail down. I'm working on a new template for a custom post type that already exists. Basically, we're replacing the single-customposttype.php file with a new one. All of that is going swimmingly, except one thing - they have one post in that custom post type that they want to keep the OLD template on.
So there's a NEW single-customposttype.php file that will work as the default single template for that CPT.
But I need ID #93 to use the OLD single-customposttype.php template. I hoped just doing single-customposttype-93.php might do the trick, but it doesn't. What's the best way to apply the other template to only one post id?
Thanks in advance!
I deal with custom template loading all the time, it's really pretty simple! Really, all you need to do is hook into the template_include hook, and override the template based on whatever conditions you want.
That hook takes a single argument, the $template file to load. You can then use any conditionals you want and force a separate file to load instead.
add_filter( 'template_include', 'custom_template_include', 99 );
function custom_template_include( $template ){
// For ID 93, load in file by using it's PATH (not URL)
if( get_the_ID() === 93 ){
// Note the file name can be ANYTHING, the WP auto-template names don't matter here
$file = get_stylesheet_directory() . '/post-id-93-custom-template.php';
// It's generally good to see if the file exists before overriding the default
if( file_exists( $file ) )
$template = $file;
}
// ALWAYS return the $template, or *everything* will be blank.
return $template;
}
It's really that simple! Inside the custom PHP file, you have access to all of the WordPress functions and such as you would with a default template.
Generally you'll want to use the file_exists() function on the template, just to make sure it's found, otherwise you'll be passing along a file that doesn't exist, and that page will not load. By checking if it exists, it will still fall back to the old template if it's not found (deleted/renamed/moved, etc)
Also, you always need to have return $template at the end, otherwise anything that uses WordPress' template system will break.
I made a quick example on a demo site:
https://xhynk.com/content-mask/policies/cookie-policy/
https://xhynk.com/content-mask/policies/use-another-template/
The policies are a custom post type, and the cookie policy loads normally. The other one is modified with the same code as above (with the name/ID changed to match), and it's loading in a simple .php file with that content in it.

Finding hooks Wordpress

I know there are lists of hooks for WordPress like --> http://adambrown.info/p/wp_hooks/hook
But if I want to find hooks for a plugin like WC Vendors there is a much shorter list of hooks on their website.
Are 'do_action' and 'apply filter' functions the only thing we can modify?
If given a class like --> https://github.com/wcvendors/wcvendors/blob/master/classes/admin/class-product-meta.php#L10, is there any way to modify it?
Are we limited to the do_action hooks or is there a way to modify other areas as well? Can we use the WordPress hooks to hook into the WC Vendors plugin as well?
Mostly you should try to accomplish any task with hooks, but some tasks are just not possible without actually modifying the actual code. But we all know its not good to modify core code, as all changes disappear after an update. So instead of modifying a class, you can extend it. You can override the current features and also add new ones. Extending a class is as easy as using a relavant hook in functions.php and then extending it in the same file or requiring it from another file. Here is an official tutorial on how to add a new shipping method to the woocommerce shipping class.
Sometimes you dont even need all the hooks, you just need to find the ones that are running on a specific page. For this you can use the code below to list all the current hooks.
$debug_tags = array();
add_action( 'all', function ( $tag ) {
global $debug_tags;
if ( in_array( $tag, $debug_tags ) ) {
return;
}
echo "<pre>" . $tag . "</pre>";
$debug_tags[] = $tag;
} );
Or you can use this plugin "simply show hooks"which is really helpful while development as it gives you an idea of where each hook is being triggered on the page.

Modifying $post->post_content in 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);

What's the best way to insert HTML chunk via WordPress plugin function?

I am creating WordPress plugin and I want to be able to provide an interface (to the programmer that installs the plugin) through some registered function in WP that when called in the template prints a HTML chunk inside the page.
What's the best way of doing it? I don't want to use echo to print large chunk of HTML from inside the function. The ideal would be having a .html file or .php file with the HTML code in it and my plugin function would simply load it when the function is called.
Does that make sense? Should I be using widgets API for this instead?
Thank you.
Something like this, which I use for an rss feed. if you remove the is feed conditional clause you should be able to just add content.
/*-----------*/
/* Add custom feed content footer
/*-----------*/
function add_feed_content($content) {
if(is_feed()) {
$content .= '<p>*This post was first published on...*</p>';
$content .= '<footer><p>*Add some Content here*</p></footer>';
}
return $content;
}
add_filter('the_excerpt_rss', 'add_feed_content');
add_filter('the_content', 'add_feed_content');
You might also need to create an admin page with a form to store the content and retrieve it if it likely to change, or just hard-code it here if it is a one-off.

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).

Resources