Load custom page templates in WordPress admin dashboard - php

I have several custom page templates in my theme. But I want to hide a few with a plugin, and only show the custom home page template on the page that has been set as the front page "is_front_page" or "is_home". This is what I am using in my plugin to stop some of the page templates from showing up.
This is for a large multisite where there are two tiers of sites, one gets the full set of features, and the other a stunted set. I have everything working as I need except for this.
add_filter( 'theme_page_templates', 'je_remove_page_template' );
function je_remove_page_template( $pages_templates ) {
unset( $pages_templates['page-topimage.php'] );
unset( $pages_templates['page-home.php'] );
return $pages_templates;
}
The code above works totally fine, but I need a conditional in there to show the page-home.php when the page has been set to the home page. I have tried this code, but it doesn't work.
if ( is_front_page() ) :
add_filter( 'theme_page_templates', 'je_remove_page_template' );
function je_remove_page_template( $pages_templates ) {
unset( $pages_templates['page-topimage.php'] );
return $pages_templates;
}
else :
function je_remove_page_template( $pages_templates ) {
unset( $pages_templates['page-topimage.php'] );
unset( $pages_templates['page-home.php'] );
return $pages_templates;
}
endif;
Any ideas on how I can get this to work?

A few things:
One, it appears you've only added the add_filter call to the is_front_page()'s true condition, that's probably part of it.
Second, you should not be defining and/or redefining functions inside if statements!
Third, for WordPress specifically, with it's Action Hook and Filter system, it's generally considered a best practice to add the filters and actions at all times, and run if/else or abort type statements inside the function to allow for easier extensibility.
Edit:
Based on some clarification, I understand a bit better. You need to get the ID of the page that's set to the front page, which is stored as an option named page_on_front in the options table. The is_front_page() function will only work in the scope of the current $wp_query loop.
I think this would solve it for you. Run the je_remove_page_template function on the theme_page_templates filter, and check the current page's ID against the one stored in the page_on_front option:
add_filter( 'theme_page_templates', 'je_remove_page_template' );
function je_remove_page_template( $pages_templates ){
unset( $pages_templates['page-topimage.php'] );
$home_page_id = absint( get_option( 'page_on_front' ) );
$this_page_id = get_the_ID();
// If this isn't the home page, remove `page-home`
if( $this_page_id != $home_page_id ){
unset( $pages_templates['page-home.php'] );
}
return $pages_templates;
}
Also of note would be that is_front_page() is different than is_home() - make sure you're using the correct one! (I believe you are, as generally IFP is what people want)

Related

Redirect User based on page template used

i have wordpress blog. I use different templates for blog posts and medical cases. I installed plugin for creating custom templates per post and it do their job. But now want medical cases to be available just for logged in users.
Page that i want to manage is using my custom template:
So i search in google for function that will limit access by template and by logged in status. And writen this function:
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
if ( is_page_template( $template = 'templates/clinic_case.php' ) && is_single() && ! is_user_logged_in() ) {
wp_redirect( 'url/wp-login.php', 301 );
exit;
}
}
but when try to load, content is loading just fine, and should not. Any help?
My guess is that by the time Wordpress processes your code, it has already passed the template_redirect hook.

Genesis Conditional Hooks Not working What am I doing wrong?

So I am trying to call the genesis_do_nav hook so its only on the front page and the homepage. For some reason it doesn't work unless I do
add_action( 'genesis_entry_header', 'genesis_do_nav' );
But that is going to execute the menu on the entry header globally which is exactly what I am trying to avoid. Any suggestions
remove_action( 'genesis_after_header', 'genesis_do_nav' );
add_action( 'genesis_entry_header', 'menu_only_on_homepage' );
function menu_only_on_homepage() {
if( is_home() && is_front_page() ){
genesis_do_nav();
}
}
Two Ideas I would like you to try:
A. First Issue I see is with your condition i.e. is_home() && is_front_page() I think this condition will never be true as both are different pages. So maybe you wanted to have it is_home() || is_front_page()
B. Try Re Arranging the code, Try arranging your conditional in different order so that you have condition before you add the hook i.e.
if(is_home() || is_front_page() ){
add_action( 'genesis_entry_header', 'genesis_do_nav' );
}
See if it helps in your situation please note code is untested as I wrote here so first test it on dev environment before trying on live so you could easily fix any typos or syntax errors.

Wordpress - Load Custom Post Type on front-page.php

I'm looking to load a single custom post type as my homepage. Not an archive but a single post, ID 10190. Looking to do this without a redirect for SEO reasons. What would be the simplest way to achieve this?
Try this
add_action( 'template_redirect', 'so_48395149' );
function so_48395149() {
global $post;
if( is_front_page() ) {
$post = get_post( 10190 );
}
}
Note: I haven't tested this, but you'll be wanting something along these lines.
WordPress stores the currently viewed post in a global $post variable. In this case, we want to overwrite that with the specified post [type] only on the front page. We use the template_redirect hook as it's one of the last hooks available before the HTML is generated.

How would you enqueue scripts in wordpress to show only on single posts & blog pages?

How would you enqueue a script in a theme that you only want to show on the blog and single posts?
I checked other questions here but didn't get convincing answer.
I got the following code from Wordpress site from a question as :
function enqueue_files() {
if ( is_page( 'your-page' ) ) {
// enqueue specific page script files here
} else {
// enqueue common scripts here
}
}
add_action( 'wp_enqueue_scripts', 'enqueue_files' );
So after taking a look at is_page() function I am confused that as I need to only show them at single posts & blog pages and while the following function would work only for static pages and since I want it to be dynamic for all of single posts pages and blog pages so how would I be able exactly to do that then with which function?
Use is_singular. It combines is_page() with is_single().
Linky.
It'll also activate on attachment pages though. If that's a problem for you, just use is_page() || is_single().
You don't need to pass the page/post slug (and it'll actually break what you are trying to accomplish. So you just do:
if ( is_page() || is_single() ) {
// if ( is_singular() ) { // or this if you prefer. :)
// enqueue specific page script files here
}
If you want to detect the blogroll, use is_home, I thought you were only targeting single posts.
Link.
Enqueue specific scripts only for the blog homepage and single posts of post type post:
function enqueue_files() {
if ( is_singular('post') || is_home() ) {
// enqueue specific scripts for blog homepage and single posts of post type post
} else {
// enqueue common scripts here
}
}
add_action( 'wp_enqueue_scripts', 'enqueue_files' );
Explanation
is_singular('post') checks if a singular post of specified post type post is being displayed (thanks #Umair Shah Yousafzai for this hint)
is_home() determines if the query is for the blog homepage

wordpress functions.php - use different page template for each post category

I want to hook into the save_post function, find out what category the post is in, and then assign a different page template for posts in each category. I've tried about 30 different versions of this with no luck. Will someone please help point me in the right direction?
add_action( 'save_post', 'assign_custom_template' );
function assign_custom_template($post_id) {
$category = get_the_category($post_id);
$cat_id = $category->cat_ID;
if( $cat_id == 1 ) {
update_post_meta($post_id, "_wp_page_template", "template1.php");
}
if( $cat_id == 2 ) {
update_post_meta($post_id, "_wp_page_template", "template2.php");
}
}
You just need to create category-1.php which rendered as template1.php and category-2.php which rendered as template2.php in your theme root.
See template hierarchy for more info.
I tried to emulate the official WP hierarchy scheme among my posts & custom post types, but it just wasn't happening. I ended up using Custom Post Types so that I could assign templates to both the "list" pages and the "individual" pages. And then I wrote some javascript that looks for the post-type string in the URL, and if it's detected, it adds the current_page_parent/ancestor classes to the appropriate menu items. Not perfect or totally future-proof, but it gets the job done.
If someone comes up with a better solution, please post it!

Categories