Iv'e been searching for a while now and my search so far has been very unsuccessful. I'm looking for a function that deletes the featured image and post when clicking delete post on the posts page in the dashboard.
What I would like: Clicking delete on a post deletes instantly along with it's featured image instead of being moved to the "trash".
Sorry I'm not a pro developer, I'm a noob and just starting out. Any help would be great. Thank you!
In any case, wp_delete_attachment requires attachment_id and not post_id. saved you half an hour ;)
add_action( 'wp_trash_post', 'delete_post_permanently' );
function delete_post_permanently( $post_id ){
wp_delete_post($post_id, true); // deletes post
//wp_delete_attachment ( $post_id, true ); // deletes attachment
if( has_post_thumbnail( $post_id ) )
{
$attachment_id = get_post_thumbnail_id( $post_id );
wp_delete_attachment($attachment_id, true);
}
}
You could use add_action hook into delete_post function, and run a function that will deelte the featred image after getting it by the post id.
https://codex.wordpress.org/Plugin_API/Action_Reference/delete_post
You can attach the Wordpress function for "before deleting a post"...
So under your theme's function.php file:
function delete_associated_media($id) {
// check if page
if ('page' !== get_post_type($id)) return;
$media = get_children(array(
'post_parent' => $id,
'post_type' => 'attachment'
));
if (empty($media)) return;
foreach ($media as $file) {
// pick what you want to do
wp_delete_attachment($file->ID);
unlink(get_attached_file($file->ID));
}
}
add_action('before_delete_post', 'delete_associated_media');
This will delete all the attachments (images) on that page being removed. You will have to tweak it just to be the featured image instead, however it might help you figure it out.
You can use wp_trash_post action hook, it runs onn post, page, or custom post type is about to be trashed
add_action( 'wp_trash_post', 'delete_post_permanently' );
function delete_post_permanently( $post_id ){
wp_delete_post($post_id, true); // deletes post
wp_delete_attachment ( $post_id, true ); // deletes attachment
}
Related
I have a custom post called ‘project’.
When a user logs in, post is automatically created.
When the post is created, I need to automatically add a featured image (only one image: number 6120) for all posts.
I tried the following code but it doesn’t add a featured image.
I’m a beginner so I’m not good at coding, would you please let me know how to solve this problem?
function wpsites_auto_set_featured_image() {
global $post, $post_type;
if( $post_type == "project" ) {
$featured_image_exists = has_post_thumbnail($post->ID);
if (!$featured_image_exists) {
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, '6120');
wp_reset_query();
}
}
}
}
}
add_action('save_post', 'wpsites_auto_set_featured_image');
Thank you.
Use save_post_{$post->post_type} for particular post type. check below code.
function wpsites_auto_set_featured_image( $post_id ) {
if ( !has_post_thumbnail( $post_id ) ) {
$thumbnail_id = 6120;
update_post_meta( $post_id, '_thumbnail_id', $thumbnail_id );
}
}
add_action( 'save_post_project', 'wpsites_auto_set_featured_image', 10 );
How do I set a default product thumbnail on WooCommerce even if there is an existing product image on a product, but will not replace or change the existing one on the database? Hope someone can help, thank you in advance.
All right so let me explain the solution for you. You have to remove product loop original image thumbnail with the remove action. Then you have to insert new action which is calling the custom thumbnail. Don't forget to replace URL OF YOUR IMAGE HERE with your image URL. For a single product you have to do the same. This code goes into functions.php file of your theme. Tested and works.
//Product loop
function custom_image_woo() {
// Remove original product image from product loop
remove_action( 'woocommerce_before_shop_loop_item_title', 'custom_image_woo', 10 );
// Your custom image thumbnail function
function here_is_the_magic() {
echo '<img src="URL OF YOUR IMAGE HERE">';
}
add_action( 'woocommerce_before_shop_loop_item_title', 'here_is_the_magic', 10 );
}
add_action( 'woocommerce_init', 'custom_image_woo');
// Single product page
//this removes product featured image
add_filter('woocommerce_single_product_image_thumbnail_html', 'main_image_away', 10, 2);
function main_image_away($html, $attachment_id ) {
global $post, $product;
$featured_image = get_post_thumbnail_id( $post->ID );
if ( $attachment_id == $featured_image )
$html = '';
return $html;
}
//this will add your image before gallery
function gallery(){
echo '<img src="URL OF YOUR IMAGE HERE">';
}
add_action( 'woocommerce_product_thumbnails', 'gallery', 10 );
This one worked for me:
function alterImageSRC($image, $attachment_id, $size, $icon) {
$image[0] = 'http://newimagesrc.com/myimage.jpg';
return $image;
}
add_filter('wp_get_attachment_image_src', 'alterImageSRC', 10, 4);
I am calculating custom title for a product using WooCommerce add product page. After the user post product's information, title is generated and saved by a save_post filter hook.
add_filter('save_post', 'modify_post_title', '99', 1);
function modify_post_title($post_id)
{
// some logic to form a new $title
// ...
if (!empty($title)) {
// update the title in database
$wpdb->update($wpdb->posts, array('post_title' => $title), array('ID' => $post_id));
// UPDATE PERMALINK
}
}
I need to know what function to use to re-generate the permalink after updating title.
Thanks in advance
add_filter( 'wp_insert_post_data', 'custom_slug_change', 50, 2 );
function custom_slug_change( $data, $postarr ) {
//Check for the post statuses you want to avoid
if ( !in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
$data['post_name'] = sanitize_title( $data['post_title'] );
}
return $data;
}
Would you please add above code in your functions.php ?
I think you can go with the window.history.pushState to maipulate the Browser History.
I think these may help you.
window.history.pushState("object or string", "Title", surl[0]);
I'm new on wordpress and i'm trying to create an archive page for multiple custom post type.
I have two custom post type, 'fair' and 'exhibition'
I currently have two archive page, with pre get post action :
add_action( 'pre_get_posts', function ( $q )
{
if ( !is_admin() // VERY important, targets only front end queries
&& $q->is_main_query() // VERY important, targets only main query
&& $q->is_post_type_archive( 'fair' ) // Which post type archive page to target
) {
$q->set( 'oderby', 'meta_value_num' );
$q->set( 'meta_key', '_datepicker');
$q->set('showposts', 3);
$q->set('is_post_type_archive', false);
$q->set('order', 'ASC');
// Rest of your arguments to set
}
});
both of the custom post type have the same meta box and i want to display both post type on the archive page "fair" or in a new one if necessary
i tried to add
$q->set('post_type', array('fair', 'exhibition'));
to the function.
When i do that i have my both post type but my custom archive page ("archive-fair.php") do not seems to be called
How can i perform the archive page for both post type properly with functional pagination ?
Thanks and sorry for my english, i hope it's understandable.
Edit : my archive-post.php page look like :
<?php get_header();
while ( have_posts() )
{
the_post();
$p_type = get_post_type(get_the_ID());
if ($p_type == 'fair')
$img = get_post_meta(get_the_ID(), "wp_fair_attachment", true);
else
$img = get_post_meta(get_the_ID(), "wp_exhibition_attachment", true);
?>
some html
<?php } ?>
An alternative approach is to use template_include
Try this code and let me know if it works:
add_filter( 'template_include', function( $template ){
$your_types = array( 'fair', 'exhibition' );
$post_type = get_post_type();
if ( ! in_array( $post_type, $your_types ) )
return $template;
return get_stylesheet_directory() . '/single-fair.php';
});
When an author clicks "update" on a post in the dashboard, how would I have the posts author change automatically to whatever author it was?
I'm trying to use this code to trigger something when a post is updated but nothing happens. Any Ideas?
add_action( 'publish_post', 'changeAuthor' );
function changeAuthor($post_id){
echo "hello";
}
this could be the function to call...
code ist not tested.
add_action('save_post', 'functiontocall');
functiontocall () {
if ( ! wp_is_post_revision( $post_id ) ){
$my_post = array(
'ID' => $post_id,
'post_author' => get_current_user_id(),
);
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'functiontocall');
// update the post, which calls save_post again
wp_update_post( $my_post );
// re-hook this function
add_action('save_post', 'functiontocall');
}
}
Did some more research and got an answer:
To make sure you hit the right action use the following
add_action('edit_post', 'functiontocall');
add_action('save_post', 'functiontocall');
add_action('publish_post', 'functiontocall');
add_action('edit_page_form', 'functiontocall');
Also, do not test this by echoing something because of some way wordpress redirects the echo will not appear! But anything else works :)