We created a plugin with a new template and we want to hook the main theme function and return the main plugin function.
We tried it with:
add_filter( "page_template", "test" );
function test( $template ) {
if( 'plugin_name.php' == basename( $template ) )
$template = WP_PLUGIN_DIR . '/plugin_folder/plugin_name.php';
return $template;
}
and changed page template in theme functions with main function of plugin which runs template inside plugin:
add_filter( "page_template", "main_plugin_function" );
Is page_template the right filter to change theme template?
Thanks for help!
I think you should use template_include filter, this filter hook is executed immediately before WordPress includes the predetermined template file. This can be used to override WordPress's default template behavior.
For example
add_filter( 'template_include', 'portfolio_page_template', 99 );
function portfolio_page_template( $template ) {
if ( is_page( 'portfolio' ) ) {
$new_template = locate_template( array( 'portfolio-page-template.php' ) );
if ( '' != $new_template ) {
return $new_template;
}
}
return $template;
}
https://codex.wordpress.org/Plugin_API/Filter_Reference/template_include
Related
I have develop a custom template for woocommerce checkout page and added all templates that I need to change specifically order_review.php in plugin/woocommerce/checkout/order_review.php and on order page it works perfect.
From Order page I can remove some products or add products through ajax and here is my ajax code.
ob_start();
woocommerce_order_review();
$woocommerce_order_review = ob_get_clean();
$response = array(
'cart_total' => WC()->cart->total,
'cart_item_key' => $new_key,
'fragments' => apply_filters(
'woocommerce_update_order_review_fragments',
array(
'.woocommerce-checkout-review-order-table' => $woocommerce_order_review,
)
),
);
if ( ! empty( $data ) ) {
$response['cartflows_data'] = $data;
}
return $response;
And the woocommerce_order_review(); loads woocommerce default template instead of template from my plugin.
WooCommerce does not look in plugin folders for templates by default. You need to use the filter woocommerce_locate_template in your plugin to tell WooCommerce to look in your plugin folder for templates. The solution is outlined by SkyVerge in this blog post: https://www.skyverge.com/blog/override-woocommerce-template-file-within-a-plugin/
I am including the solution here in case the post is every removed for some reason:
function myplugin_plugin_path() {
// gets the absolute path to this plugin directory
return untrailingslashit( plugin_dir_path( __FILE__ ) );
}
add_filter( 'woocommerce_locate_template', 'myplugin_woocommerce_locate_template', 10, 3 );
function myplugin_woocommerce_locate_template( $template, $template_name, $template_path ) {
global $woocommerce;
$_template = $template;
if ( ! $template_path ) $template_path = $woocommerce->template_url;
$plugin_path = myplugin_plugin_path() . '/woocommerce/';
// Look within passed path within the theme - this is priority
$template = locate_template(
array(
$template_path . $template_name,
$template_name
)
);
// Modification: Get the template from this plugin, if it exists
if ( ! $template && file_exists( $plugin_path . $template_name ) )
$template = $plugin_path . $template_name;
// Use default template
if ( ! $template )
$template = $_template;
// Return what we found
return $template;
}
You have to override WooCommerce Template
woocommerce/templates/checkout/review-order.php and Put this file in your theme folder themes/yourthemename/woocommerce/checkout/review-order.php
I built a custom template page named Single Listing. permalink for this page is / homes /
But I'd like to show this custom template page for all the urls formatted like this homes/*
I am new to wordpress so any help or suggestions would be welcome.
Thanks.
Not tested, but this might be helpful for you.
add_filter( 'template_include', 'portfolio_page_template', 99 );
function portfolio_page_template( $template ) {
global $wp;
$current_url_path = home_url( $wp->request );
$url_pattern = '/(^homes[\/\w]*)/gim'
if ( is_page() && preg_match($pattern, $current_url_path)) {
$new_template = locate_template( array( 'home-page-template.php' ) );
if ( '' != $new_template ) {
return $new_template ;
}
}
return $template;
}
i'm use this code:
add_filter( 'page_template', 'calendar_load' );
function calendar_load( $template ) {
if ( is_page( 'calendar' ) ) {
$template = dirname( __FILE__ ) . '/calendar-page.php';
return $template;
}
}
He is workin if my page name "calendar"
But i want see my templete in page attributes in wp theme. How i can do it?
enter image description here
I am trying to add a custom stylesheet to the TinyMCE editor in a WordPress plugin I am developing. The WP codex tells me to use the mce_css filter, but it does not seam to work.
As soon, as I use the filter, all the theme's custom stylesheets disappears from the editor, but my custom stylesheet is still not there.
See the following two screenshots, the first without the filter, the second with the filter activated:
Here is my code:
class test_plugin {
function __construct($args = array()){
if ( is_admin() ){
add_action('admin_head', array( $this, 'admin_head') );
add_action( 'admin_enqueue_scripts', array($this , 'admin_enqueue_scripts' ) );
}
}
function admin_head() {
if ( !current_user_can( 'edit_posts' ) && !current_user_can( 'edit_pages' ) ) {
return;
}
if ( 'true' == get_user_option( 'rich_editing' ) ) {
add_filter( 'mce_css', 'plugin_mce_css' );
}
}
function admin_enqueue_scripts(){
// wp_enqueue_style('fa_icon_shortcode', plugins_url( 'css/mce-button.css' , __FILE__ ) );
}
function plugin_mce_css( $mce_css ) {
if ( ! empty( $mce_css ) )
$mce_css .= ',';
$mce_css .= plugins_url( 'editor.css', __FILE__ );
return $mce_css;
}
}
new test_plugin();
Any idea what goes wrong here?
It actually was a pretty simple solution. Since I am using OOP, I had to add the filter with
add_filter( 'mce_css', array( $this , 'plugin_mce_css' ) );
instead of
add_filter( 'mce_css', 'plugin_mce_css' );
Works like charm!
i created a custom page named custom_page.php in wordpress and use this as a page template.
i wanted to create my own functions and only target my custom_page.php. this function will be installed as a plugin.
what kind of WP HOOKS should i use, for example i have this code below
function your_function() {
if ( is_page_template('custom_page.php') ) {
echo '<p>This is inserted at the bottom</p>';
{
}
add_action('wp_footer', 'your_function');
i want to only execute this code in my custom_page.php footer area.
[EDIT]
You can use these hooks for page templates.
http://adambrown.info/p/wp_hooks/hook/page_template
Or may be something like this...
add_filter( 'page_template', 'check_the_template' );
function check_the_template( $template ) {
if( 'custom_page.php' == basename( $template ) ) {
// do some things here by calling functions, hooks etc..
}
return $template;
}
If you want to just echo "Hello World" :
add_filter( 'page_template', 'check_the_template' );
function check_the_template( $template ) {
if( 'pagetemplete.php' == basename( $template ) ) {
add_action( 'the_content', 'echofunction' );
}
return $template;
}
function echofunction() {
echo "Hello World";
}