I have customised my Wordpress site design to use the featured image for posts quite excessively. This is why I need to require all post made by non-admins to require a set featured image.
How is this possible?
You need to hook the Publish Action in a custom PlugIn you write. Although this is to require a title, this should get you started, you just need to check if a featured image was assigned.
add_action( 'pre_post_update', 'bawdp_dont_publish' );
function bawdp_dont_publish()
{
global $post;
if ( strlen( $post->title ) < 10 ) {
wp_die( 'The title of your post have to be 10 or more !' );
}
}
Look at (has_post_thumbnail( $post->ID )) to determine if a post has a featured image.
Given Gary's example above, I've written the following to my functions.php file:
function featured_image_requirement() {
if(!has_post_thumbnail()) {
wp_die( 'You forgot to set the featured image. Click the back button on your browser and set it.' );
}
}
add_action( 'pre_post_update', 'featured_image_requirement' );
I'd much rather see this in a plugin as well - there's one called Mandatory Field but it doesn't work with scheduled posts. Both are not really eloquent solutions.
you can use a plugin
https://wordpress.org/plugins/require-featured-image/
or you can copy and paste below code in your wordpress theme functions.php file:
<?php
/**
* Require a featured image to be set before a post can be published.
*/
add_filter( 'wp_insert_post_data', function ( $data, $postarr ) {
$post_id = $postarr['ID'];
$post_status = $data['post_status'];
$original_post_status = $postarr['original_post_status'];
if ( $post_id && 'publish' === $post_status && 'publish' !== $original_post_status ) {
$post_type = get_post_type( $post_id );
if ( post_type_supports( $post_type, 'thumbnail' ) && ! has_post_thumbnail( $post_id ) ) {
$data['post_status'] = 'draft';
}
}
return $data;
}, 10, 2 );
add_action( 'admin_notices', function () {
$post = get_post();
if ( 'publish' !== get_post_status( $post->ID ) && ! has_post_thumbnail( $post->ID ) ) { ?>
<div id="message" class="error">
<p>
<strong><?php _e( 'Please set a Featured Image. This post cannot be published without one.' ); ?></strong>
</p>
</div>
<?php
}
} );
Related
I am currently trying to use the first image of an ACF gallery field as featured image.
The plugin I use is called Frontend Admin, it allows users to create posts from the frontend.
While my script works in the WordPress backend, it does not work for the frontend submission:
add_action('acf/save_post', 'first_Image_featured', 50);
function first_Image_featured() {
//get important fields
$current_screen = get_current_screen(); // Current admin screen needed to identify the current cpt
$current_cpt_name = 'musterkueche'; // Current CPT name
$current_cpt_support = 'thumbnail'; // Check if the CPT supports this feature
global $post;
$post_id = ( $post->ID ); // Current post ID
$post_gallery_field = get_field('musterkuchengalerie', $post_id ); // ACF Image Gallery field
if ( !empty( $post_id ) ) {
if ( isset( $post_gallery_field['0'] ) ) {
$post_image_id = $post_gallery_field['0']['id']; // ACF image filed ID
$post_image_url = $post_gallery_field['0']['url']; // ACF image filed URL
// If current CPT supports thumbnails/featured images
if ( post_type_supports( $current_cpt_name, $current_cpt_support ) ) {
if ( ( $post_image_url ) AND ( ( $post_image_url ) != ( get_the_post_thumbnail() ) ) ) {
update_post_meta($post_id, '_thumbnail_id', $post_image_id);
}
}
} else {
update_post_meta( $post_id, '_thumbnail_id', 0 );
}
}
}
The Plugin Developer of Frontend Admin said:
Our frontend forms don't fire the acf/save_post hook, use do_action( 'frontend_admin/save_post', $form, $post_id ); instead.
$form (array) The form settings, including $form['record']['post'] which contains all of the submitted post fields’ data.
$post_id (int|string) The ID of the post being edited.
acf/save_post comparison for Frontend Admin
This hook is a bit different from acf/save_post so be aware of these differences:
$post_id is the second of two parameters, so make sure to include
both parameters and to set the fourth argument of add_action to “2”.
This hook is always fired after the fields have been saved so none of
the fields will be in the $_POST global but rather within the
$form['record']['fields'][‘post'] array.
Example Function for Frontend Admin (For sending email after post is saved)
add_action('frontend_admin/save_post', 'my_frontend_save_post', 10, 2);
function my_frontend_save_post( $form, $post_id ) {
//get important fields
$post_content = get_post_field('post_content',$post_id);
$post_title = get_post_field('post_title',$post_id);
$email_address = get_field('email_address', $post_id);
if( $email_address ) {
$email_sent = wp_mail( $email_address, $post_title, $post_content );
}
}
My second approach (dosent work):
add_action('frontend_admin/save_post', 'first_Image_featured', 10, 2);
function first_Image_featured ($form, $post_id) {
//get important fields
$current_screen = get_current_screen(); // Current admin screen needed to identify the current cpt
$current_cpt_name = get_post_type('musterkueche'); // Current cpt name
$current_cpt_support = 'thumbnail'; // Check if the CPT supports this feature
global $post; // This needs to be changed, but I dont understand the Documentation
$post_id = ( $post->ID ); // Current post ID
$post_gallery_field = get_field('musterkuchengalerie', $post_id); // ACF Image Gallery field
if ( !empty( $post_id ) ) {
if ( isset( $post_gallery_field['0'] ) ) {
$post_image_id = $post_gallery_field['0']['id']; // ACF image filed ID
$post_image_url = $post_gallery_field['0']['url']; // ACF image filed URL
// If current cpt supports thumbnails/featured images
if ( post_type_supports( $current_cpt_name, $current_cpt_support ) ) {
if ( ( $post_image_url ) AND ( ( $post_image_url ) != ( get_the_post_thumbnail() ) ) ) {
update_post_meta($post_id, '_thumbnail_id', $post_image_id);
}
}
} else {
update_post_meta( $post_id, '_thumbnail_id', 0 );
}
}
}
I have customised my Wordpress site design to use the featured image for posts quite excessively. This is why I need to require all post made by non-admins to require a set featured image.
How is this possible?
You need to hook the Publish Action in a custom PlugIn you write. Although this is to require a title, this should get you started, you just need to check if a featured image was assigned.
add_action( 'pre_post_update', 'bawdp_dont_publish' );
function bawdp_dont_publish()
{
global $post;
if ( strlen( $post->title ) < 10 ) {
wp_die( 'The title of your post have to be 10 or more !' );
}
}
Look at (has_post_thumbnail( $post->ID )) to determine if a post has a featured image.
Given Gary's example above, I've written the following to my functions.php file:
function featured_image_requirement() {
if(!has_post_thumbnail()) {
wp_die( 'You forgot to set the featured image. Click the back button on your browser and set it.' );
}
}
add_action( 'pre_post_update', 'featured_image_requirement' );
I'd much rather see this in a plugin as well - there's one called Mandatory Field but it doesn't work with scheduled posts. Both are not really eloquent solutions.
you can use a plugin
https://wordpress.org/plugins/require-featured-image/
or you can copy and paste below code in your wordpress theme functions.php file:
<?php
/**
* Require a featured image to be set before a post can be published.
*/
add_filter( 'wp_insert_post_data', function ( $data, $postarr ) {
$post_id = $postarr['ID'];
$post_status = $data['post_status'];
$original_post_status = $postarr['original_post_status'];
if ( $post_id && 'publish' === $post_status && 'publish' !== $original_post_status ) {
$post_type = get_post_type( $post_id );
if ( post_type_supports( $post_type, 'thumbnail' ) && ! has_post_thumbnail( $post_id ) ) {
$data['post_status'] = 'draft';
}
}
return $data;
}, 10, 2 );
add_action( 'admin_notices', function () {
$post = get_post();
if ( 'publish' !== get_post_status( $post->ID ) && ! has_post_thumbnail( $post->ID ) ) { ?>
<div id="message" class="error">
<p>
<strong><?php _e( 'Please set a Featured Image. This post cannot be published without one.' ); ?></strong>
</p>
</div>
<?php
}
} );
So, I'm developing a site for a client where the homepage is built like a one-page scroller, but I also need the functionality of additional pages outside of the single homepage. I've built a custom post type for these sections and used this code to display them on the homepage.
<?php query_posts( array('post_type'=>'homepage', 'posts_per_page' => 1000, 'orderby' => 'menu_order', 'order' => 'ASC') ); ?>
<?php if(have_posts()): while(have_posts()): the_post(); ?>
<?php
global $post;
$slug = $post->post_name;
locate_template(
array(
"template-$slug.php",
'template-main.php'
), true
);
?>
<?php endwhile; endif; ?>
So, as you can see, this is automatically pulling content and displaying it using page templates based on the post slug, however, I need to allow my client to display the content based on a page template chosen in a dropdown and I've used this code to create a dropdown UI that displays the page templates.
add_action( 'add_meta_boxes', 'add_custom_page_attributes_meta_box' );
function add_custom_page_attributes_meta_box(){
global $post;
if ( 'page' != $post->post_type && post_type_supports($post->post_type, 'page-attributes') ) {
add_meta_box( 'custompageparentdiv', __('Template'), 'custom_page_attributes_meta_box', NULL, 'side', 'core');
}
}
function custom_page_attributes_meta_box($post) {
$template = get_post_meta( $post->ID, '_wp_page_template', 1 ); ?>
<select name="page_template" id="page_template">
<?php $default_title = apply_filters( 'default_page_template_title', __( 'Default Template' ), 'meta-box' ); ?>
<option value="default"><?php echo esc_html( $default_title ); ?></option>
<?php page_template_dropdown($template); ?>
</select><?php
}
add_action( 'save_post', 'save_custom_page_attributes_meta_box' );
function save_custom_page_attributes_meta_box( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) return;
if ( ! current_user_can( 'edit_post', $post_id ) ) return;
if ( ! empty( $_POST['page_template'] ) && get_post_type( $post_id ) != 'page' ) {
update_post_meta( $post_id, '_wp_page_template', $_POST['page_template'] );
}
}
So, the problem I'm facing now is how to display all the custom posts in my main page according to the chosen page template.
Thanks so much!
J
Wordpress actually only uses _wp_page_template meta field for page type posts. If you want to change the template, you can use the filter single template. One thing I would recommend is you place good notes that you are using this in your theme/ plugin....
btw update cpt to your post-type
function load_cpt_template($single_template) {
global $post;
if ($post->post_type == 'cpt') {
$new_template = get_post_meta( $post->ID, '_wp_page_template', true );
// if a blank field or not valid do nothing, load default..
if( is_file($new_template) )
$single_template = $new_template;
}
return $single_template;
}
add_filter( 'single_template', 'load_cpt_template' );
This is what I want to accomplish. In Wordpress I've created a taxonomy called categorie with the terms app, web and branding. When a project has the term app, I want to load another theme / blog. When a project has the term web or branding, I want to load single.php. The last one works just fine.
This is my code so far
function load_single_template($template) {
$new_template = '';
if( is_single() ) {
global $post;
if( has_term('app', 'categorie', $post) ) {
$new_template = get_theme_roots('themeApp');
} else {
$new_template = locate_template(array('single.php' ));
}
}
return ('' != $new_template) ? $new_template : $template;
}
add_action('template_include', 'load_single_template');
So when a project has the term app, I want to load the theme themeApp. Any suggestions? Thanks in advance.
We had to accomplish a similar task in our plugin, AppPresser. You can see our solution here: https://github.com/WebDevStudios/AppPresser/blob/master/inc/theme-switcher.php
Basically, you need to change the theme name in 3 filters: 'template', 'option_template', 'option_stylesheet'.
Getting the category is not so simple though, because the template check happens early enough in the WordPress process that the global $post and $wp_query objects are not available.
Here is one way that can be accomplished:
<?php
add_action( 'setup_theme', 'maybe_theme_switch', 10000 );
function maybe_theme_switch() {
// Not on admin
if ( is_admin() )
return;
$taxonomy = 'category';
$term_slug_to_check = 'uncategorized';
$post_type = 'post';
// This is one way to check if we're on a category archive page
if ( false !== stripos( $_SERVER['REQUEST_URI'], $taxonomy ) ) {
// Remove the taxonomy and directory slashes and it SHOULD leave us with just the term slug
$term_slug = str_ireplace( array( '/', $taxonomy ), '', $_SERVER['REQUEST_URI'] );
// If the term slug matches the one we're checking, do our switch
if ( $term_slug == $term_slug_to_check ) {
return yes_do_theme_switch();
}
}
// Try to get post slug from the URL since the global $post object isn't available this early
$post = get_page_by_path( $_SERVER['REQUEST_URI'], OBJECT, $post_type );
if ( ! $post )
return;
// Get the post's categories
$cats = get_the_terms( $post, $taxonomy );
if ( ! $cats )
return;
// filter out just the category slugs
$term_slugs = wp_list_pluck( $cats, 'slug' );
if ( ! $term_slugs )
return;
// Check if our category to check is there
$is_app_category = in_array( $term_slug_to_check, $term_slugs );
if ( ! $is_app_category )
return;
yes_do_theme_switch();
}
function yes_do_theme_switch( $template ) {
// Ok, switch the current theme.
add_filter( 'template', 'switch_to_my_app_theme' );
add_filter( 'option_template', 'switch_to_my_app_theme' );
add_filter( 'option_stylesheet', 'switch_to_my_app_theme' );
}
function switch_to_my_app_theme( $template ) {
// Your theme slug
$template = 'your-app-theme';
return $template;
}
I am using a WooCommerce plugin for WordPress. Now in a file page.php I am trying to get the current opened page id via $post->ID and the_id() but all I am getting is the product ID and not the current page ID. Probably because WooCommerce overwrites the loop ?
What can I do ?
It might be too late, but i was trying to get the same result as you and this below worked pretty well for me:
<?php
if(is_shop()){
$page_id = woocommerce_get_page_id('shop');
}else{
$page_id = $post->ID;
}
?>
Hope this will help you.
if you are trying to get the particular page id. Try with this.
if ( wc_get_page_id( 'name of page' ) != get_the_ID()):
This worked fine for me
global $post;$post->ID; or global $wp_query;$wp_query->post->ID; don't work with woocommerce. You can get all the id's of woocommerce page here
You can try this, inspired by woocommerce_product_archive_description
add_action('woocommerce_after_main_content', 'custom_name', 11, 2);
function custom_name() {
if ( is_post_type_archive( 'product' ) && 0 === absint( get_query_var( 'paged' ) ) ) :
global $post;
$shop_page = get_post( wc_get_page_id( 'shop' ) );
if ( $shop_page ) {
$post = $shop_page;
}
// your code here
endif;
}
$woo_pages_id = array(
//get_option( 'woocommerce_shop_page_id' ),
get_option( 'woocommerce_cart_page_id' ),
get_option( 'woocommerce_checkout_page_id' ),
//get_option( 'woocommerce_pay_page_id' ),
//get_option( 'woocommerce_thanks_page_id' ),
get_option( 'woocommerce_myaccount_page_id' ),
//get_option( 'woocommerce_edit_address_page_id' ),
//get_option( 'woocommerce_view_order_page_id' ),
get_option( 'woocommerce_terms_page_id' )
);
$current_page_id = get_the_ID();
if ( in_array($current_page_id, $woo_pages_id) ) :
get_template_part('content/woo', 'pages');
else :
get_template_part('content', 'page');
endif;
I'm using this for my page.php file.
put this line above your code or at starting of script.
global $post;
now you can use $post->ID;
thanks
Had the same problem found a solution here
The code I used on my shop page was:
<?php echo woocommerce_get_page_id('shop'); ?>
Basically replace the shop value with whatever woocommerce page you are trying to get.
More documentation on the function and some other helpful functions can be found here