template_include filter does not render the custom template

I created a plugin that will render a shortcode that is entered to a page. This Shortcode shall override the theme's page template and use the one that I included on the plugin Here's my code: api.php class Api{ public static $logger = null; function Api() { add_shortcode('my_shortcode', array(&$this, 'my_shortcode_function')); } function my_shortcode_function($atts,$content = null) { add_filter( 'template_include', 'custom_view'); } function custom_view() { $template = plugin_dir_path( __FILE__ ) . 'custom-page.php'; return $template; } } add_action('init', 'apiInit', 10); function apiInit() { …
Category: Web

Replace wp_query with wp_user_query

I'm turning out some subscriber archives and singles (profile pages) in my plugin where I'd also included a 'user-single' template, included via template_include. I'm experimenting however with trimming some of these templates out of my plugin in order to make it use theme templates. I've instead used locate_template( 'single.php' ) to choose the single template from the active theme. I'm not using the global wp_query to turn out my content in this case but the page shows a loop based …
Category: Web

Pull in an "Include" file based on a WordPress Category ID

Here's what I am trying to achieve: when a category page is loaded then, based upn the ID of that category, an "include" file is loaded. I believe that the below would work for some limited text, but I would prefer to load an "include" file b/c it will have multiple paragraphs and I feel it is better to manage that way. <?php $catarray = get_the_category( $post->ID ); foreach ($catarray as $cat) { $catid = $cat->term_id; if ($catid == 10) …
Category: Web

Use TEMPLATEPATH for custom url

I have a code in below and as you can see I have a condition to read custom template for desktop and mobile. Now I want to use a custom url instead of a file.For example I want to include a page site.com/test instead of index.php <?php // Use anywhere if ( muneeb_wp_is_mobile() ){ include(TEMPLATEPATH . '/index-mobile.php'); } else{ include(TEMPLATEPATH . '/index-desktop.php'); } ?> Can I use this? include(TEMPLATEPATH . '/site.com/page1');
Category: Web

How to use template_include hook with form submission?

I'm writing a plugin that uses the template_include hook to use a custom template for a custom post type. This works with GET requests, but with POST requests, it 404s because the $post variable is null within this hook in POST requests. How do I fix this so that I can use this custom template for both GET and POST requests? namespace mynamespace; class MyPlugin { public static function template_include($template) { global $post; var_dump($post); //$post exists for GET requests but …
Category: Web

template_include doesn't work with child theme

I am using this code and it works perfectly but it does not work if I activate the child theme how can I solve? add_filter( 'template_include', 'wpse_template_include' ); function wpse_template_include( $template ) { // Handle taxonomy templates. $taxonomy = get_query_var( 'taxonomy' ); if ( is_tax() && $taxonomy ) { $file = get_theme_file_path() . '/template-parts/taxonomies/taxonomy-' . $taxonomy . '.php'; if ( file_exists( $file ) ) { $template = $file; } } return $template;
Category: Web

For custom templates, is it better to use `template_include` or `type_template`?

I saw a lot of questions about including custom views in Wordpress (I mean custom templates). There were many answers. Some people recommend using template_include and others recommend using type_template My question is, which is better for working with custom templates? What are the pros and cons of both solutions?
Category: Web

How to include a page template and template part into my plugin

I am creating my first plugin with a template for the frontend. My current structure is: /my_plugin/views/page_templates I have 2 main issues I am trying to resolve Is this the best way to include a template How to include external files into template using get_template_part() The template code: function mbbp_frontend_page_layout($page_template) { global $post; if ( is_page("bookings") ) { $page_template = MBBP_BOOKING_PLUGIN_DIR_PATH . '/views/page-bookings.php'; } return $page_template; } add_filter("page_template", "mbbp_frontend_page_layout"); And then, within page-bookings.php I have included: <?php get_template_part( 'page-templates', 'payment-form' …
Category: Web

Should I use include(_once) or require(_once) for loading php-file with customizer settings?

Should I use include, include_once, require or require_once to load my customizer.php file? That file has all customizer settings and code in it. What is your advice on this? The file is currently being loaded with include() in the functions.php. And can I use a fallback? For all situations that I can't predict, when the customizer.php file fails to load?
Category: Web

Link to file in plugin directory from wordpress template?

I know that plugins and theme files should be kept separate but for for internal use I need to do it this way. In my themes header.php file I want to include a php file which just contains html, from my plugin directory. The path is basically /wp-content/plugins/my_plugin/my-html.php I can't seem to figure out the proper code for wordpress to look in the plugins directory, and grab the my-html.php file from within the my_plugin folder. I want to include this …
Category: Web

Put CSS inside a PHP file and include it the right way

I want to put CSS inside a PHP file, because with that I can use 'conditional CSS', depending on the value of a customizer control. My idea is to create a new php file, somewhere in my theme directory ./assets/css/header-conditional-styles.css In this file I have created the following code: <?php header("Content-type: text/css; charset: UTF-8"); ?> <style> <?php if ( text == get_theme_mod( 'layout_logo', text ) ) : ?> .theme-header-title { color: green !important; } <?php else : ?> .theme-header-title { …
Category: Web

Should I use do_action in the header file?

I have a lot to add on the header file because of if/else condition. And it makes the file very messy. So, I have decided to include another file... So how to add it: Creating a function file and add it with do_action() Getting a template file get_template_part()
Category: Web

Render a different post/template from within the template_include action?

I am working on a webinar plugin that uses a custom post type to display the webinar pages. Those pages could be register, thank you, countdown, live etc. depending on the visitors registration status and the current webinar status. The plugin uses the template_include action to render content based on the current post status and the visitors status (if they are registered or not). The plugin lets users choose a custom page for one of those webinar pages, like a …
Category: Web

Include Template Based on User IP Address

I need to prohibit certain IP Address from visiting my wordpress site. Hence i have used below code in my child theme function.php add_action('template_include', 'restrict_user_access'); function restrict_user_access() { $ipAddress = $_SERVER['REMOTE_ADDR']; if($ipAddress === 'XX.XX.XXX.XXX') { return "home/www/html/blog/wp-content/themes/theme-child/restrictusers.php"; } } } I can able to load this template & display the content for this IP whereas for other IP Address i get empty page. Am i missing any? My wordpress version is 4.9.10. I also need to replicate this same process …
Category: Web

Redirect page to homepage, keeping URL

I'm creating a site where every page is really rendered on a single page - the homepage. I'd like to make it so page URLs - like /about-us/ - redirect back to the homepage, but keep the same URL (/). For example, if I visit http://example.com/about-us/, I want to see http://example.com/ (scrolled down to the correct section, thanks to some JS), but with the URL still as http://example.com/about-us/. The template_include action seems like it'd work, but doing this: function home_template($template) …
Category: Web

How to add variables to a template

I am using a query argument detail to check if a custom template should be loaded. This works fine, but I also need to add some variables that can be accessed from within the template. Here I am using add_action('template_include' array($this, 'templateInclude'): public function templateInclude($template) { if (get_query_var('detail', false) !== false) { // Check theme directory first. $newTemplate = locate_template(array('detail.php')); if ($newTemplate != '') return $newTemplate; // Check plugin directory next. $newTemplate = plugin_dir_path(__FILE__) . 'templates/detail.php'; if (file_exists($newTemplate)) { return …
Category: Web

How do I go about looping through a advanced custom field on a particular page inside of another page

I have one page lets call it page-home.php and I also have a page called page-custom-field.php I want to have a section of the first page where it loops through page-custom-field.php page and display it on itself. Here is my code attempting that so far. I am using advanced custom fields on the second page. My If statements page-home.php <?php $args = array( 'post_type' => array( 'features' ), 'posts_per_page' => 1, ); $query = new WP_Query( $args ); ?> <div …
Category: Web

Why isn't template_include working with AMP correctly?

Before you ask I have looked around the similar questions to find a resolution but I couldn't find one that worked. Ive tried the permalink option several times, There is nothing in my htaccess file that would be causing the 404 There's no caching software installed There are no other plugins that im using for redirection or amp The is_single() works perfectly fine but the is_page() does not, i've also tried is_front_page(), is_category(), is_home(), is_page('specific page') but again dont seem …
Category: Web

Content vs Template on custom post

I just wrote a custom post (suppliers) as a plugin (with meta content) that works great. I've also created a template page (single-suppliers.php) within the same plugin to display the data. I wrote it in such a way that, if someone so desires, they can create their own single-suppliers.php page in their template and get their own look and feel. (I'm using template_include filter to do this.) Example of the template_include filter. // Register a my Membership Template. function my_membership_include_template_function( …
Category: Web

Why is WP template_include overwritting all templates rather than specified page?

I have created a custom plugin for WP that has an action that includes a template via template_include as shown here: public function __construct( $wp_custom_plugin, $version ) { $this->wp_custom_plugin = $wp_custom_plugin; $this->version = $version; add_action('template_include', array( $this, 'add_my_template' )); } Here is the function that references the template: public function add_my_template($template){ if ( is_page( 'my-unique-page' ) ) { $template = plugin_dir_path( __FILE__ ) . 'page-my-unique-page.php' ; return $template; } } } Okay, so my expectation here is that i would …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.