1

here is what i have tried

function mec_locations_cpt_add_content ( $data, $postarr) {        //, $postarr
    if ( 'mec_locations_cpt' !== $postarr['post_type'] ) {
         return $data;
    }
    if ( 'publish' !== $postarr['post_status'] ) {
         return $data;
    }
    $post = get_post( $postarr['ID'] );
    //$mec_location_shortcode='[mec-location id=”{LOCATION_ID}” limit_events=”{NUMBER}”]';        //to get from an admin field
    //$mec_location_shortcode ='<!-- wp:shortcode -->[mec-location id=128]<!-- /wp:shortcode -->';
    $data['post_content'] .= '<!-- wp:shortcode -->[mec-location id=128]<!-- /wp:shortcode -->';
    $data['post_content'] .= '<!-- wp:paragraph -->#####################################################'.'post='.print_r($post,true).'<!-- /wp:paragraph -->';
    $data['post_content'] .= '<!-- wp:paragraph -->@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'.'data='.print_r($data,true).'<!-- /wp:paragraph -->';
    $data['post_content'] .= '<!-- wp:paragraph -->&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&'.'postarr='.print_r($postarr,true).'<!-- /wp:paragraph -->';
    //$data['post_title']   .= 'some text';
    //$data['post_excerpt']  = 'This is my excerpt inserted for new post';
    // If value is not in content, append it onto the end
    //if ( stristr( $data['post_content'], $content_shortcode ) === FALSE ) $data['post_content'] .= 'add this'.$meta_value .($meta_value);
    //$postarr['post_title'] .='add some text';
    //$postarr['post_content'] .='ok ! added ' . $mec_location_shortcode;
    //$postarr['post_excerpt']  ='My new updated excerpt';
    return $data;
}
add_filter('wp_insert_post_data', 'mec_locations_cpt_add_content' ,100 ,2);

but asap i try to add a new post, i have an error message La sauvegarde de cet article dans votre navigateur est différente de la version ci-dessous. meaning ~ : the post is diferent than the one in browser

when i edit title and content and try to publish the post it give a json error La publication a échoué. La réponse n’est pas une réponse JSON valide. I have tried to change permalink setting with no success i get the same error without adding any content

but the strange is if i cancel publish the post is saved in db and the array are included twice... if i edit again the post, it fires again a json error but the post content is updated again

Does it mean that wp_insert_post_data is fired before insert and again at the save_post ????

i know that making it this way will add content each time. i planed to add some check content before adding it but for now i try to remove only the error message

1 Answer 1

0

You should use content_save_pre filter. And maybe try some "simple" content first.

wp_insert_post_data is fired in a lot of situations :-) Even at deleting.

1
  • hi, i will try rewrite using it as it seems i will not be able to use wp_insert_post_data. but i don't undestand why it make this json error when i only try to add a basic and "simple" word like just "hello" in content
    – ari elbaz
    Feb 22, 2022 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.