Forum Replies Created

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter lollike

    (@lollike)

    OK thanks – I’ll be eagerly awaiting πŸ˜‰

    Thread Starter lollike

    (@lollike)

    OK – that didn’t work either… πŸ™
    I created the following function in a new plugin:

    function save_my_post_test_plugin(){
    	global $post;
    	$post = get_post($post->ID);
    
    	/* Set the post title */
    	$title = 'My TEST plugin post';
    
    	/* Set the post content */
    	$content = 'This post was created by '.__FUNCTION__;
    
    	/* Prepare array for creating the WordPress post */
    	$my_post = array(
    		'ID'		=> $post->ID,
    		'post_type'	=> 'post',
    		'post_title'	=> $title,
    		'post_content'	=> $content,
    		'post_status'	=> 'private'
    	);
    
    	/* Unhook this function so it doesn't loop infinitely */
    	remove_action('save_post', 'save_my_post_test_plugin');
    	/* Update the post (which calls save_post again) */
    	wp_update_post($my_post);
    	/* Re-hook the save_post function */
    	add_action('save_post', 'save_my_post_test_plugin');
    }

    First I tried calling the function like this:
    add_action('rest_api_inserted_post', 'save_my_post_test_plugin');
    That didn’t trigger the function when the post was created…

    Then I tried to call the function like this:
    add_action('wp_insert_post', 'save_my_post_test_plugin');
    That didn’t trigger the function when the post was created either…

    Finally I called the function like this:
    add_action('save_post', 'save_my_post_test_plugin');
    And that didn’t trigger the function when the post was created either (except, of course, when I re-saved it; When the post was re-saved, the function was finally triggered).

    For clarification, I sent 1 new mail for each try.

    Also, I remind you that I can’t use the save_post as my initial problem is, that I want to modify the post before the post it’s saved for the first time due to the Publicize restrictions – e.g. I want to modify the slug (and other stuff) before the post is shared on the social media πŸ˜‰

    By the way, while calling the function with wp_insert_post I also tried to create a new post from inside the WP Admin Panel, and that did trigger the function the first time I saved it…!

    Hope this helps!? πŸ™‚

    Thread Starter lollike

    (@lollike)

    Do you mean that I should try creating another function doing something simple and trigger it by add_action('wp_insert_post', 'my_new_simple_function');?

    Thread Starter lollike

    (@lollike)

    If you mean hook into like calling my function like this: add_action('rest_api_inserted_post', 'save_snowforecast');
    that doesn’t work either…
    Best regards Tomas Lollike

    Thread Starter lollike

    (@lollike)

    Thanks for the quick answer πŸ™‚

    Unfortunately it didn’t change anything. The post still gets created without triggering the function – but it does still get triggered, however, when I subsequently go into the post and re-save it (just like before with save_post).

    I have poked a little into the REST API Resources (which is new to me) and in turn into Create a post but it’s not mentionned how the post actually gets created from there, and thus what action to hook into.

    Any ideas on that or other suggestions on what else to try?

    Thread Starter lollike

    (@lollike)

    Sorry for the late answer…! πŸ™
    I have moved the functionality into a plugin as suggested, but there’s no change.
    When posting by e-mail the post is still generated, but without triggering the plugin function. When entering the post and re-saving it – the function is triggered correctly.

    My understanding of the plugin is, that when posting the e-mail, it is routed via WordPress.com and then posted into my own WordPress?

    So could it be, that WordPress.com reads my configuration.php and with the DB-information posts directly into the DB omitting the API, and thus not triggering the add_action(‘save_post’, ‘save_snowforecast’)?

    Thread Starter lollike

    (@lollike)

    Thanks for your answer csonnek πŸ™‚
    I’m not sure your solution is useful – maybe I didn’t explain myself clearly enough;

    When I receive a certain type of e-mail (a snow forecast e-mail), I want to be able to forward it to my WordPress as a post in the “SnowCast” category.
    The goal is to click “Forward e-mail”, and only add the [category = SnowCast] before sending to WordPress.
    When the e-mail is received by WordPress and saves it to the blog, I want the below function (placed in functions.php) to trigger.

    What happens now, is that the e-mail is inserted at a post, but the function doesn’t trigger – until I go into the newly created post, and save it manually (then it works perfectly).

    However, since I use Publicize to various social media, I need the function to trigger on the first save, when the post is first created (as this is the only time Publicize triggers).

    As it is now the publicized post NOT publicizable – hence, I have to put the most important stuf from the function into shortcodes before sending the e-mail – that does the job, but is tedious, as it is always the same tags to be out in, and because the publicized post viewd from a social media sees the top of the e-mail (commercials and other irrelevant stuf) which I remove with my function…

    Also, editing in the original e-mail (to remove the irrelevant stuff at the top and bottom) when forwarding one, is not always possible on a mobile phone e-mail app – so I often have to wait until I get to a “real” computer πŸ™

    SO, as said – I want to forward the e-mail only with the category tag, and have the function to take care of the rest, at the first time the post i saved into WordPress:

    Function in functions.php I want to trigger:

    add_action('save_post', 'save_snowforecast');
    function save_snowforecast(){
      global $post;
      $post = get_post($post->ID);
      if(in_category('SnowCast')){
        /* Get content of the post */
        $content = $post->post_content;
        $content = apply_filters('the_content', $content);
        $content = preg_replace('#<a.*?>([^>]*)</a>#i', '$1', $content);
    
        /* Get string between "3 Day Forecast" and "About this report" and put it back to $content */
        preg_match('/3 Day Forecast(.*?)About this report/s', $content, $matches);
    
        /* Add "3 Day Forecast" in front of $content and put the combined string into $post_content */
        $post_content = '3 Day Forecast '.$matches[1];
    
        /* Get the date of the original SnowForecast and format it to dd-mm-yyyy*/
        preg_match('/Issued:(.*?)local time/s', $content, $matches);
        $date_content = $matches[1];
        $forecastdate = explode(' ', $date_content);
        $hours = substr($forecastdate[1],-2);
        $hours = str_replace('>','0',$hours);
        if($forecastdate[2] == 'pm'){
          if($hours != 12) $hours = $hours +12;
    	 }else{
          if($hours == 12) $hours = $hours -12;
        }
        $month = substr($forecastdate[5],0,3);
        $month = strtotime($month);
        $month = date('m', $month);
        $post_year = (get_the_date( 'Y', $post_id )) ? get_the_date( 'Y', $post_id ) : date('Y') ;
        $timestamp = $post_year.'-'.$month.'-'.intval($forecastdate[4]).' '.$hours.':00:00';
    
        /* Set the $post_title */
        $post_title = 'SnowCast for Tignes - '.$forecastdate[4].'-'.$month.'-'.$post_year;
        /* Set the Excerpt */
        $post_excerpt = 'Crystal crisp SnowCast for Tignes on Lollike.dk :-)';
        /* Prepare array for creating the WordPress post */
        $my_post = array(
          'ID'            => $post->ID,
          'post_type'     => 'post',
          'post_title'    => $post_title,
    		'post_name'     => $timestamp,
          'post_content'  => $post_content,
          'post_excerpt'  => $post_excerpt,
          'post_date'     => $timestamp,
        );
    
        /* Unhook this function so it doesn't loop infinitely */
        remove_action('save_post', 'save_snowforecast');
        /* Update the post (which calls save_post again) */
        wp_update_post($my_post);
        /* Re-hook the save_post function */
        add_action('save_post', 'save_snowforecast');
    
        /* Add post thumbnail to post if none is already existing */
        if (!has_post_thumbnail($post->ID)){
          update_post_meta( $post->ID, '_thumbnail_id', '4927');
        }
    
        /* Set tags of the post */
        wp_set_post_tags($post->ID, 'Tignes,SnowCast,Weather,Snow,Tignes SnowReport,Tignes WeatherForecast', FALSE);
      }
    }

    Please feel free to ask, if I still didn’t make my self clear enough πŸ™‚
    Thanks again! πŸ™‚

Viewing 7 replies - 1 through 7 (of 7 total)