Support » Plugin: Polylang » [Plugin: Polylang] Suggestion for "save_post" action in "admin-filters.php"

  • Greetings Chouby.

    In my custom made theme, on the admin front, I have an ajax request which changes the post_status (from “draft” to “publish”, for example).

    if($_POST["ajax_do"]) { add_action('wp_ajax_my_theme_ajax', 'my_theme_ajax'); }
    
    function my_theme_ajax() {
      if(!check_ajax_referer('is-this-my-ajax?')) exit;
    
      if($_POST["ajax_do"] == "publish-post" && $_POST["post_id"]) {
        $post = array('ID'=>(int)$_POST["post_id"], 'post_status'=>'publish');
        $post_id = wp_update_post($post);
        if(!$post_id) ajax_respond(array('status'=>0,'msg'=>'Erro a publicar o passeio.'));
        ajax_respond(array("status"=>1, "html"=>my_theme_passeios_agendados_list(), "msg"=>"Passeio publicado com sucesso"));
      }
    }

    The problem: I was loosing the language setting for this post when calling wp_update_post($post)

    Reason: in “admin-filters.php” the “save_post” action assumes that posts are only update from the edit post page, where there is a $_POST[‘post_lang_choice’] coming our way. But that’s not always the case.

    Suggestion: around line 338 in “admin-filters.php” use this code instead:

    // save language
    if (isset($_POST['post_lang_choice']))
    	$this->set_post_language($post_id, $_POST['post_lang_choice']);
    elseif (isset($_GET['new_lang']))
    	$this->set_post_language($post_id, $_GET['new_lang']);
    else {
    	if(($current_lang = $this->get_post_language($post_id)->slug)) $this->set_post_language($post_id, $current_lang);
    	else $this->set_post_language($post_id, $this->options['default_lang']);
    }

    As you can see it’s a very minor change, just one line added, that checks if the post being updated doesn’t already have a language setting, before using the default language.

    I hope it makes to the next release of Polylang 🙂

    Thank you and keep up the good work with this excellent language plugin!

    http://wordpress.org/extend/plugins/polylang/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Chouby

    (@chouby)

    This is an excellent idea ! Thank you very much. Consider I will add it to 0.8.4. I will just simplify the code like this:

    if (isset($_POST['post_lang_choice']))
    	$this->set_post_language($post_id, $_POST['post_lang_choice']);
    elseif (isset($_GET['new_lang']))
    	$this->set_post_language($post_id, $_GET['new_lang']);
    elseif ($this->get_post_language($post_id))
    	{}
    else
    	$this->set_post_language($post_id, $this->options['default_lang']);

    Of course! That’s one of the reasons I choose you language plugin over the others. I liked you simplicity 🙂

    I believe this change will also have an impact on making this plugin more compatible with other plugins and themes.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Plugin: Polylang] Suggestion for "save_post" action in "admin-filters.php"’ is closed to new replies.