How to build custom plugin to integrate with current theme - wordpress

I am developing a plugin with a CPT and a CPT template. I would like the template to be able to integrate into any theme it is used with. I thought of calling the 'the_content' hook from my template but I have no idea how to do that as all I can find is calling it from the functions.php file. Please can you tell me how to code this in the CPT template file (like single-CPT.php) or maybe I am heading in the wrong direction so please redirect me. Thank you!

EDIT:
If you only want to add content to the_content function you can do something like that:
add_filter('the_content', "test_the_content");
function test_the_content($content){
if(is_singular('test')){
$content = $content.test_additional_content();
}
return $content;
}
function test_additional_content(){
ob_start(); ?>
<div class="test">
Here what you want to display after the_content
</div>
<?php
return ob_get_clean();
}
ORIGINAL ANSWER
If I understood your question correctly, here is how to define the templates of your custom post type in your plugin. This way it will also be possible to overwrite the plugin template from the theme.
The example below is for a CPT called "test", so you have to adapt the code according to the name of your CPT.
add_filter('template_include', 'my_plugin_templates');
function my_plugin_templates($template) {
$post_types = array('test');
if (is_post_type_archive($post_types) && !file_exists(get_stylesheet_directory() . '/archive-test.php')) {
$template = plugin_dir_path( __FILE__ ) . 'archive-test.php';
}
if (is_singular($post_types) && !file_exists(get_stylesheet_directory() . '/single-test.php')) {
$template = plugin_dir_path(__FILE__) . 'single-test.php';
}
return $template;
}
You can also take a look to this repo: https://github.com/zecka/cpt-in-plugin

Related

Custom post template in a plugin erase theme's one

i work with sportpress on Wordpress. I have a custom plugin that add a lot of function to it.
I created a template for the custom post-type of sportpress. They are in my plugin but i don't find how to make them prioritary on theme. The theme isn't a custom'one, so i don't want to delete or modify anything in it.
I have tried several things but theme's file is everytime superior.
Things tried :
naming the file 'single-....php',
filter : add_filter( 'template_include', 'toornament_template' ),
shortcode in theme file (works but not what i want...),
Thanks a lot...
EDIT :
here is what i tried exaclty, it works with another type of post that has no theme template, but not for the one that has single-post template in the theme...
add_filter( 'template_include', 'player_template', 1 );
function player_template( $template ) {
if ( is_singular( 'sp_player' ) && file_exists( plugin_dir_path(__FILE__) . 'templates/player-tpl.php' ) ){
$template = plugin_dir_path(__FILE__) . 'templates/player-tpl.php';
}
return $template;
};

Wordpress custom button link output after the_content

I'm trying to insert a button to show at the end of each post on Wordpress in which the link it goes to is defined by a setup using the custom fields plugin. When creating each post, I am able to select the link I wish to display.
Here is the code I have which I know is wrong but I was hoping someone could help here.
function wpb_after_post_content($content){
if (is_single()) {
$content .= 'Contact Franchise →';
}
return $content;
}
add_filter( "the_content", "wpb_after_post_content" );
I assume $franchise_profile_url is a variable and you should concatenate it in the string like this
$content .= 'Contact Franchise →';
function afterContent($content) {
if(!is_feed() && !is_home()) {
$content.= "<div class='footNote'>";
$content.= "<h4>Click the below button if you like it:</h4>";
$content.= "<p><a href='#'>LIKE</a></p>";
$content.= "</div>";
}
return $content;
}
add_filter ('the_content', 'afterContent');
Use the above function. It will help you to achieve what you need.
Thanks for the help here, however, that code simply links back to the post itself and isn't pulling in the URL as set on the post using custom fields. This is the code I had set up before which was working on a default post setup but now I wish to use an alternative method in the functions.php file
<?php if( get_field('franchise_profile_url') ): ?>
Contact Franchise →
<?php endif; ?>

Hook taxonomy template to category template

I've created a plugin which registers a custom post type and a custom taxonomy for me.
Now, whenever I visit: www.mysite.com/events_categories/category I should get a list of all the posts under that category. I want to use my own template and not Wordpress' default category template. Also, I want this template to be used for ALL categories, not just one category.
I used this code:
function taxonomy_template() {
global $post;
if( is_tax( 'event_categories' ) ) {
$tax_tpl = dirname( __FILE__ ) . '\taxonomy-event_categories.php';
}
return $tax_tpl;
}
add_filter( 'template_include', 'taxonomy_template' );
And while it works for the taxonomy, it makes everything else on the site go blank. This is because the $tax_tpl is empty if the page I'm on isn't a taxonomy.
I've tried using template_redirect, but no luck either.
So I want to know how to hook my template (taxonomy-event_categories.php) to the Wordpress' category template.
Hope you guys have a solution, because I've looked everywhere and it's possible to find the solution, yet I see many plugins doing this effortlessly.
Okay, so I solved it using this code:
add_action('template_redirect', 'taxonomy_template');
function taxonomy_template( ){
$tax_tpl = dirname( __FILE__ ) . '\taxonomy-event_categories.php';
if( is_tax('event_categories') ) {
include $tax_tpl;
die();
}
}
I've no idea why this works because I think I already tried this specific code, but it didn't work. Let's just hope it will keep working.

How do I get the name of the file that is being used to render the current page?

Let's say I have a WordPress installation, with a page named "About". If I go to http://example.com/about, I know from WordPress' template hierarchy page that I'm looking at the theme file page.php.
I'm wondering if there's a way to display that fact (for theme debugging) on the page somewhere? Like what function (or code) would I call to display the current PHP page that is being used to render the page I'm looking at.
I could do something with $_SERVER['PHP_SELF'], but I'm looking for a way where I don't have to edit every PHP file. Like something that spits out the list of files it's using as the pages are called.
It can be printed in the Html source code like this:
add_action( 'wp_head', 'so_9405896_show_template', 999 );
function so_9405896_show_template() {
global $template;
echo '
<!--
TEMPLATE = ' . basename($template) .'
-->
';
}
Or for easier visualization, directly in the content with this:
add_filter( 'the_content', 'so_9405896_the_content_filter', 20, 1 );
function so_9405896_the_content_filter( $content )
{
if( is_admin() || !current_user_can( 'administrator' ) )
return $content;
global $template;
$the_templ = '<strong style="background-color: #CCC;padding:10px">TEMPLATE = '
. basename( $template ) . '</strong><br />';
$content = sprintf( $the_templ . '%s', $content );
return $content;
}
Which results in:
As far as I've seen there's no built in option to enable such logging, only for errors.
I'm not sure what editor you use, but most common text editors allow you to do a find replace across an entire folder.
I'd suggest doing a temporary replace on includes and require's to add an echo of the PHP_SELF. Just make sure to add a comment or something before the echo so that you can easily replace them with nothing when you're done.
A quick search on the WordPress plugin repository brings up a WordPress Debug Bar Template Trace.
I just manually type it into the template, i.e. ARCHIVE.PHP, CATEGORY-1.PHP when I'm building it. Just remember to delete it once the site goes live.
Simple and easy, if not so graceful.
For those looking for a newer answer:
<?php if ( is_user_logged_in() ) { global $template; echo basename($template); } ?>
This checks if a user is logged in, then only shows the template name. This can be handy if you need to hack your way on a live site without adding functions.

Do I have to add_filter() before apply_filters() in Wordpress?

I'm trying to understand Wordpress plugin like:
apply_filters( 'gettext', $translations->translate( $text ), $text, $domain );
I'm looking for all codes in Wordpress, I can't find:
add_filter( 'gettext', ....);
Why there is no add_filter for this plugin? Or I missed something? Same thing like:
do_action('wp_loaded');
I can't find:
add_action('wp_loaded', ....);
apply_filters is like, 'if there are any filters with this name, run the attached callbacks with these parameters'. So if there is no add_filter for that name, it means that there is no filter that's going to be run with the apply_filters call at the moment.
The same goes with do_action and add_action.
I am a beginner in PHP - WordPress stack as well, but this is from my understanding.
The plugins call apply_filters without having any add_filter in their codes is to allow the website users to add custom logic to their plugins. We - the users, can add our own function and use add_filter to register our functions.
For example, this piece of code is from the plugin. Normally, it shows all products but it provides us a way to not show a specific product.
// Plugin's
if (apply_filters( 'plugin_show_products', true, $product->get_id() ) ) {
$this->show_products();
}
So, if we - the users, want to customize a bit. We can add our own function as following (maybe in functions.php)
// Our custom changes
function my_own_changes($boolean, $product_id) {
if ( $product_id === 5 ) return false;
return true;
}
add_filter( 'plugin_show_products', 'my_own_changes', 10, 2 );
This translates to: The plugin will behave normally but for my own site, it will not show the product with ID of 5!
I have come across this type of code in a plugin or theme where the apply_filter is used without necessarily having an existing filter or add_filter
In this case, where the apply_filters is used without a filter you will have to call the function again where you want to run it. For example, in the header of a theme.
The following is an example of apply filters used in a function that is again called in the header.php
if ( ! function_exists( 'header_apply_filter_test' ) ) {
function header_apply_filter_test() {
$filter_this_content = "Example of content to filter";
ob_start();
echo $filter_this_content;
$output = ob_get_clean();
echo apply_filters( 'header_apply_filter_test', $output );//used here
}
}
Now in the header.php file, you would have to call this function since it is not hooked anywhere. So, in this case, to display the output in the header you would call the function like this :
<?php header_apply_filter_test(); ?>
You could as well write this code with a hook and it would do the same thing i.e display the output in the header.
add_filter('wp_head', 'header_apply_filter_test');
if ( ! function_exists( 'header_apply_filter_test' ) ) {
function header_apply_filter_test() {
$filter_this_content = "Example of content to filter";
ob_start();
echo $filter_this_content;
$output = ob_get_clean();
echo $output;
}
}
For this second option, you would still have the capability of using apply_filters anywhere else to call the callback function header_apply_filter_test() since the filter now exists.
So the bottom line in my view is a use case since either approach works!

Resources