As a wp plugin developer, I want to load a post single page from my plugin directory. Not a custom post type I want to load from the default wp post type.function.php template.php
You can use this hook to check if this is a single page and the post type is 'post'
add_action( 'single_template', [$this, 'event_single_page'] );
function event_single_page( $single ) {
global $post;
if ( $post->post_type == 'post' && is_singular( 'post' ) ) {
$plugin_template = 'post-single-page.php';
if ( file_exists( $plugin_template ) ) {
$single = $plugin_template ;
}
}
return $single;
}
Related
I am trying to manipulate an elementor query for all post types. I am using the Profile Builder plugin to restrict content. Their support sent me this function to check wether the post is restricted or not:
wppb_content_restriction_is_post_restricted( $post_id )
When I output the result of this function on the post it is either 1 or nothing. (1 being restricted).
The support also send me a custom addon-plugin of another plugin they have, where I'd only need to add the new function. When I do this and install it to my page, nothing changes though.
Here is the code of their mini plugin:
<?php
/*
Plugin Name: Paid Member Subscriptions - Exclude Restricted Posts From Query
Plugin URI: http://www.cozmoslabs.com
Description: Exclude restricted posts and pages from the main queries like blog, archive and taxonomy pages. It does not exclude them from custom queries.
Author: Cristian Antohe
Version: 1.0
Author URI: http://www.cozmoslabs.com
*/
add_action( 'pre_get_posts', 'pmsc_exclude_post_from_query' );
function pmsc_exclude_post_from_query( $query ) {
remove_action('pre_get_posts', 'pmsc_exclude_post_from_query');
if( !function_exists( 'pms_is_post_restricted' ) || is_admin() || is_single() )
return;
if( $query->is_main_query() || ( $query->is_search() && isset( $_GET['s'] ) ) ) {
$args = $query->query_vars;
$args['suppress_filters'] = true;
$args['posts_per_page'] = get_option( 'posts_per_page' );
$posts = get_posts($args);
$ids = wp_list_pluck( $posts, 'ID' );
$restricted_ids = array_filter($ids,'pms_is_post_restricted');
$query->set( 'post__not_in', $restricted_ids );
}
}
In case that a post is restricted, I want to exclude it from my query. So that the user only sees posts that can be accessed.
Now the challenge is to write a custom query that I can then put inside of the query ID of elementor's post widget to perform my custom query.
My query looks like this so far:
function prevent_restricted_posts_from_loading( $query ) {
if( !function_exists( 'wppb_content_restriction_is_post_restricted' ) || is_admin() || is_single() )
return;
if( $query->is_main_query() || ( $query->is_search() && isset( $_GET['s'] ) ) ) {
$args = $query->query_vars;
$posts = get_posts($args);
$ids = wp_list_pluck( $posts, 'ID' );
$restricted_ids = array_filter($ids,'wppb_content_restriction_is_post_restricted');
$query->set( 'post__not_in', $restricted_ids );
}
}
add_action( 'elementor/query/prevent_restricted_posts_from_loading', 'prevent_restricted_posts_from_loading' );
I want to check if is single product page, but this has no effect on single product page:
if (! is_admin() && is_product() ) {
var_dump('is product'); // this has no effect in single product page
}
Is there a limitation to use is_product() in functions.php? How can I achieve this?
EDIT:
In order to avoid var_dump issues, this is the final code I'm trying. It try to add product type (simple/variable) to body class:
add_action( 'woocommerce_after_single_product', function () {
if (! is_admin() && is_product() ) {
add_filter( 'body_class', function( $classes ) {
global $post;
$product = wc_get_product( $post->ID );
$tipo = $product->get_type();
return array_merge( $classes, array( $tipo ) );
});
}
}, 100 );
You can not use this function directly without a hook since your functions.php file is included before every other file - mostly. To make this thing work you need to work with hooks every time like this:
add_action( 'template_redirect', 'template_redirect_action' );
function template_redirect_action() {
if ( ! is_admin() && is_product() ) {
add_filter( 'body_class', function ( $classes ) {
global $post;
$product = wc_get_product( $post->ID );
$tipo = $product->get_type();
return array_merge( $classes, array( $tipo ) );
} );
}
}
Visit a product page and check your body classes.
By using the template_redirect hook you can be sure it's executed on every page. Otherwise your check would not make any sense when using a product hook which only gets executed on product pages.
Tested an works.
Sorry for my English. I created custom post type in WordPress named "Gallery" and i want password protected this page with posts. Not only posts. How i can do that? I found something like this but this is work only for single posts:
function tp_stop_guestes( $content ) {
global $post;
if ( $post->post_type == 'YOUR_CUSTOM_POSTTYPE' ) {
if ( !is_user_logged_in() ) {
$content = 'Please login to view this post';
}
}
return $content;
}
add_filter( 'the_content', 'tp_stop_guestes' );
You need to redirect visitor bu using wp_redirect() function.
function admin_redirect() {
global $post;
if ( !is_user_logged_in() && $post->post_type == 'YOUR_CUSTOM_POSTTYPE' ) {
wp_redirect( home_url('login') );
exit;
}
}
add_action('get_header', 'admin_redirect');
I want to check the post type of any pages or posts and display some features depending on the post type. So I'm using a function in functions.php which returns the post type when used in admin pages but not on theme pages.
I need this function to include a php file if the post type is page. So it must work system wide (if it's possible). In templates, admin, edit and post pages.
Here is the function that I'm using:
function vart_current_post_type() {
global $post, $typenow, $current_screen;
if ( $post && $post->post_type ) {
return $post->post_type;
}
elseif ( isset( $_GET[ 'post' ] ) ) {
return get_post_type( $_GET[ 'post' ] );
}
elseif ( $typenow ) {
return $typenow;
}
elseif ( $current_screen && $current_screen->post_type ) {
return $current_screen->post_type;
}
elseif ( isset( $_REQUEST[ 'post_type' ] ) ) {
return sanitize_key( $_REQUEST[ 'post_type' ] );
}
return null;
}
add_action( 'init', 'vart_current_post_type' );
Please tell me what should I do in order to make it work on theme templates and pages?
Thanks.
There's a native method to find out current post type: https://developer.wordpress.org/reference/functions/get_post_type/
$post_type = get_post_type();
On the init hook, the $post object is not yet populated.
See this for WP hook (action) execution. As you can see, posts are selected much later after init. To be more precise, even the request is parsed after init hook. So there is no easy way to get post type in init action, I'm afraid.
With the wp hook you can get the post type with your function.
I have plugin that create new post type. Also plugin set single template for it's single page.
add_filter( 'single_template', array( &$this, 'register_ipa_product_post_type_single_template' ) );
function register_ipa_product_post_type_single_template( $single_template ) {
global $post;
if ( $post->post_type == 'product' ) {
$single_template = IPA_PRODUCT_POST_TYPE_TEMPLATE_FOLDER . 'single-product.php';
}
return $single_template;
}
How i can override single-product.php in my theme.
I don't found any solutions for my question at this site.
just filter it a little later than the current function (ps if doing this within a class you need to reference it using array(&$this, 'function'). I left it out as i assume you are using the functions.php or function override....etc
add_filter( 'single_template', 'register_ipa_product_post_type_single_template', 100 );
function change_temp( $single_template ) {
global $post;
if ( $post->post_type == 'product' ) {
$single_template = 'path to your template file';
}
return $single_template;
};