Check wordpress shortcode is exists on home page - wordpress

Is there any way to know specific shortcode is applied on specific page?
for e.g. On any WP Theme's home page if related_products shortcode is applied then I want to check it in my plugin file that is applied or not?
Is there any way?

You can use has_shortcode() to check if your shortcode is applied. If you need to specifically check if it is used on the home page, you can add the is_home() check
global $post;
if( is_home() && has_shortcode( $post->post_content, 'related_products') ) {
echo "YEAH, shortcode is applied on home page";
}else{
echo "Houston, we have a problem!!!";
}

Related

Page template for all WooCommerce Files

I have a page template that is a minimalized page (call it min-page.php) in my theme. I use it to remove the header and footer info and add in some basic links so the customer doesn't get distracted.
I can change this template in the Page Attributes on various WooCommerce pages (checkout, cart, my account) but I can't change it on the product page or the general shopping page that displays all the products.
How can I set these pages to use my page template?
You can change the single product template by using the following code via functions.php
function get_product_template($single_template) {
global $post;
if ($post->post_type == 'product') {
$single_template = dirname( __FILE__ ) . '/min-page.php';
}
return $single_template;
}
add_filter( 'single_template', 'get_product_template' );
Check the template path after placing the code.

Different header in different pages in wordpress

I am going to develop a WordPress theme for my own site. It has three types of menu.
first one is only for the home page, the second one is for any normal page (like about us, contact etc.) and the third one is for article page (when a news will be clicked then where one will be gone).
how can I do this?
thanks.
There are multiple ways you can go about doing this. For the article I would create a PHP file named page-article.php and within the file follow what developer tells you:
<?php
/**
* Template Name: Article
*
*/
and then create a custom header for the article in a header-article.php file:
header('article') ?>
To target the home you could use is_page() and pass it home but I would suggest using is_home() instead but you could always pass both like:
if ( is_page('home') || is_home() ) :
// further code
endif;
Do note this would depend on your settings as you do not mention if you will have a custom page set by the setting under Settings -> Reading so if you set front page you should target it with is_front_page():
if ( is_page('home') || is_home() || is_front_page() ) :
// further code
endif;
You could always code all of this in one header.php file but I would suggest creating an additional header file and calling it header-article.php
You can do it using is_page() function.
See full documentation here
if( is_page( array( 'about-us', 'contact') ){
//display your page menu
}
if( is_page( 'home' ){
//display your home page menu
}
if( is_page( 'article' ){
//display your article page menu
}else{
//display default menu
}
If you need different header in different page then you have to create a page template then you can add different header file in different page template-
Normally we use get_header() to include header.php
Now create a copy of header.php file and named it header-alternative.php
Then create a page template named "Alternative header"
<?php get_header('alternative');
/*
*Template Name: Alternative Header
*/
?>
<section class="error-page-inner">
<h1 class="error-code"><?php esc_html_e( '404', 'labstore' ); ?></h1>
</section>
<?php get_footer(); ?>
Then header-alternative.php is called for this page template.

Adding content just to the landing page or a WordPress site

I want to add an image below the header of my landing page. I've managed to do this with the code
add_action( 'genesis_after_header', 'add_image_after_header' );
function add_image_after_header() {
$upload_dir = wp_upload_dir();
$upload_url = $upload_dir['url'];
$banner_img = $upload_url . "/Pelican.jpg";
echo ("<img id='banner_pic' src=$banner_img>");
}
But the image appears on every page of the site and I just want it to appear on the landing page. Also, is there a more generic "after_header" hook I can use instead of the genesis version?
Thanks for your help.
If your landing page is your home page, you can use is_home() to only add the image when the home page is shown.
add_action( 'genesis_after_header', 'add_image_after_header' );
function add_image_after_header() {
if( is_home() ) {
$upload_dir = wp_upload_dir();
$upload_url = $upload_dir['url'];
$banner_img = $upload_url . "/Pelican.jpg";
echo "<img id='banner_pic' src=$banner_img>";
}
}
For the hook, I don't think you can find another one more generic than this one (for your needs).
The Visual Hook Guide is great to see where you can hook on genesis.
EDIT
is_home() works great when your home page show your blog posts, but if it's a static page, you should use is_front_page() which will be true for either blog posts or static page.

Woocommerce change product style and display it on a page

Sorry in advance for my approximative english. I would like to change product page style on woocommerce for a specific product category (don't display pictures and some other important layout changes). Already made it and it works, the problem is that I would like to display these products on a single page. I tried using woocommerce integrated shortcode [product_page id=$id], the problem is that the custom style and layout I created on content-single-product.php (in woocommerce folder) is not applied using this shortcode. I tried to copy my script on class-ws-shortcodes.php (woocommerce folder) but it didn't worked. What should I do to display the products on a specific page, with the real style of these products page (the custom one I created), and not the woocommerce shortcode one?
Thanks in advance for your answers
There are two ways to achieve this, it just depends on whether you want to edit single-product.php or content-single-product.php. Both will get you to the same end result.
To do the former, you can use default WordPress functionality and filter the template under given conditions via template_include
add_filter( 'template_include', 'so_29984159_custom_category_template' );
function so_29984159_custom_category_template( $template ){
if ( is_single() && get_post_type() == 'product' && has_term( 'custom_category', 'product_cat' ) ) {
$template = locate_template( 'single-product-custom.php' );
}
return $template;
}
Or if you'd rather create a custom content-product.php template part, you can use WooCommerce's wc_get_template_part which works in pretty much the same way.
Technically I think you can use the same conditional logic, but I tweaked it here to point out the variables WooCommerce makes available at the filter.
add_filter( 'wc_get_template_part', 'so_29984159_custom_content_template_part', 10, 3 );
function so_29984159_custom_content_template_part( $template, $slug, $name ){
if ( $slug == 'content' && $name == 'product' && has_term( 'custom_category', 'product_cat' ) ) {
$template = locate_template( 'content-product-custom.php' );
}
return $template;
}
In either case the single-product-custom.php or content-product-custom.php would be in your theme's root folder. To put them somewhere else, just change the path in the locate_template() argument.
You can not do coding in wordpress's file i.e. page.php to modify woocommerce pages. Its wrong way. You simply override WooCommerce pages into your theme folder and then modify any pages which you want to do. All templates/pages are located in a woocommerce/templates folder.
Here you can find documentation.
http://docs.woothemes.com/document/template-structure/
edit woocommerce/templates/content-single-product.php for single product view

Require WordPress Login only for posts, not pages?

I'm trying to create a WordPress site with a login system, but I only want the login to be required to view POSTS. They should be able to view pages without logging in. I did some research, but no one had exactly what I was looking for, so I tried to puzzle something together, and this is what I came up with, but it doesn't work.
function my_force_login() {
global $post;
if ( ( is_single() && !is_user_logged_in() ) ){
auth_redirect();
}
}
Any ideas?
add it to your single.php
<?php auth_redirect(); ?>
This function will check if the user is logged in and if he is not it will redirect him to the login page http://codex.wordpress.org/Function_Reference/auth_redirect
Your code works, but it has run on the correct point:
add_action( 'template_redirect', function()
{
global $post; // Use this to detect the custom post types if needed
if ( ( is_single() && !is_user_logged_in() ) )
{
auth_redirect();
}
});
Details of the post is being displayed in the single.php. Please put the code of displaying post's content into the condition. Then you will get the solution. Try the following code into the single.php in the twentyforteen theme.
if ( is_user_logged_in() ) {
get_template_part( 'content', 'single' );
}
Now get logged out and refresh the browser, you can see none of content are shown.

Resources