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 :)
Related
I need to save some options when a woocommerce product gets saved.
Is there any way to do it?
You could use save_post action hook!
save_postDocs
You could use this boilerplate to do your custom work when a product gets saved!
add_action('save_post', 'do_some_custom_work');
function do_some_custom_work( $post_id )
{
if ('product' == get_post_type($post_id)) {
// Do what is necessary here!
die('You hit the right hook!!!');
}
}
Note:
In addition to $post_ID, you could pass $post and $update to your callback function as well. Please read the documentation for more details!
add_action('save_post', 'do_some_custom_work', 10, 3);
function do_some_custom_work( $post_id, $post, $update )
{
if ('product' == get_post_type($post_id)) {
// Do what is necessary here!
die('You hit the right hook!!! This is the product id: ' . $post->ID);
}
}
I try to add my products with as little human input as possible.
Therefor I'm looking for a solution to grab the title tag which is in my added product image and put it in the Product name field on or before saving the product. Any attempts to achieve this are failing because WordPress "thinks" that no title is given (so no slug could be generated). At least I think that this is the case.
See screenshot of the field
I tried to use a code snippet I found here on SO and to rework it to a working solution but I fail to get it right.
Here is the code I came up with:
function fcsp_set_title_on_save( $post_id ) {
$post_thumbnail_id = get_post_thumbnail_id( $post_id );
$filemeta = wp_get_attachment_metadata( $post_thumbnail_id, FALSE );
// Set this variable to false initially.
static $updated = false;
// If title has already been set once, bail.
if ( $updated ) {
return;
}
// Since we're updating this post's title, set this
// variable to true to ensure it doesn't happen again.
$updated = true;
$title = $filemeta['image_meta']['title'];
// Update the post's title.
wp_update_post( [
'ID' => $post_id,
'post_title' => $title,
] );
}
add_action( 'save_post', 'fcsp_set_title_on_save' );
Any idea how to accomplish this?
Please put this code in function.php if this is woocommerce you are using (I think rather than custom post type )
if(class_exists('WC_Admin_Meta_Boxes')) {
class wcsave extends WC_Admin_Meta_Boxes {
public function __construct() {
add_action( 'save_post', array( $this, 'save_meta_boxes' ), 1, 2 );
add_action( 'woocommerce_process_product_meta', 'WC_Meta_Box_Product_Data::save', 10, 2 );
}
public function save_meta_boxes( $post_id, $post ) {
//$_POST enter your post data here, this will help to control the post request from product woocommerce
//if product is updating don't execute image title code section
if(!empty($post->post_title)) {
return;
}
//if new product is being added.
if(!empty($_POST['post_ID']) && $post_id == $_POST['post_ID']) {
$post_thumbnail_id = get_post_thumbnail_id( $post_id );
$attachment_data = get_post( $post_thumbnail_id,OBJECT );
$title = count($attachment_data) > 0 ? $attachment_data->post_title : "PRODUCT-".$post_id;
remove_action( 'save_post', array( $this, 'save_meta_boxes' ) , 1, 2 );
wp_update_post( [
'ID' => $post_id,
'post_title' => $title,
'post_status' => $post->post_status
] );
// re-hook this function
add_action( 'save_post', array( $this, 'save_meta_boxes' ) , 1, 2 );
}
}
}
new wcsave();
}
But please note, as I've tested you need at least some other info along with product image you are uploading like product description, rest it will save the image name as product title.
Thanks
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]);
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
}
I'm trying to make a function where a child post of a certain post type inherits the same title and slug as its parent. I'm using WP Types to define my post types and their relationships. But I'm having trouble with the below code:
function copy_parent_post_title( $post_id ) {
$new_post = get_post($post_id);
if($new_post->post_type == 'carnews-adverts') {
$parent_id = get_post_meta( $post_id, '_wpcf_belongs_carnews_id', true );
$parent_title = get_the_title($parent_id);
$post_slug = sanitize_title_with_dashes($parent_title);
$post_update = array(
'ID' => $post_id,
'post_title' => $parent_title,
'post_name' => $post_slug
);
remove_action( 'wp_insert_post', 'copy_parent_post_title' );
wp_update_post( $post_update );
add_action( 'wp_insert_post', 'copy_parent_post_title' );
}
}
add_action( 'wp_insert_post', 'copy_parent_post_title' );
The problem is this line:
$parent_id = get_post_meta( $post_id, '_wpcf_belongs_carnews_id', true );
I presume it is because at this point the post's meta data hasn't been inserted into the database yet? If so how can I achieve what I want by accessing the get_post_meta upon inserting a post?
Thanks
I think to access the '_wpcf_belongs_carnews_id' that is being added by WP Types, you need to look in the $_POST array. However, WP Types might call add_post_meta() after calling wp_insert_post(). In this case the meta data won't be present if you hook into wp_insert_post.
Instead, hook your function into add_post_meta:
function copy_parent_post_title( $post_id, $meta_key, $meta_value ) {
$new_post = get_post($post_id);
if (($new_post->post_type == 'carnews-adverts') &&
($meta_key == '_wpcf_belongs_carnews_id')) {
$parent_id = $meta_value;
$parent_title = get_the_title($parent_id);
// ... Rest of your function
}
}
add_action( 'add_post_meta', 'copy_parent_post_title', 10, 3 );
This may be completely wrong as I barely ever use Wordpress.