• Resolved manex

    (@manex)


    Hi,

    I am developing a plugin for my website(WordPress version 3.8.2) that when adding or editing posts has the capability of translating the content of those posts using web-services. I found some plugins with that functionality but they are designed for using Google translate, which is now a paid service, so they are not what I am looking for.

    Up to now, I added a button to the post editor that opens a pop-up in which I will have the translation capabilities, such as, choose source and target language etc. This is the code I used for the button.

    function oi_itzultzaile_botoia_gehitu()
    	{
    		echo '<a href="'.get_bloginfo("wpurl").'/wp-content/plugins/ohar-itzultzailea/itzulpen-orria.php?TB_iframe=true"
    				class="thickbox" title="'.__("Ohar Itzultzailea","ohar-itzultzailea").'">
    					<img src="'.get_bloginfo("wpurl").'/wp-content/plugins/ohar-itzultzailea/irudiak/itzuli-botoia.png" ></img>
    			</a>';
    
    	}
    
    	add_action('media_buttons', 'oi_itzultzaile_botoia_gehitu');

    The popup works fine. Now, I am trying to get the content in the post editor, so that I can use it as a parameter for the web-service. However, no luck so far. I have been looking into the documentation and browsed into the results given by google but I did not find something that would help me. Does anybody know how to do it?

    Thanks for your help, πŸ˜€

    Manex

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    The content is not available server side until the user submits the form. Thus translating from your server must be done after the form is submitted.

    You can use javascript or jQuery to get the current value of the editor container. jQuery can then query the translate API and update the container value without any need for a page reload. This is essentially an AJAX technique even though the response may not actually be XML. JSON is actually easier to deal with IMO.

    Thread Starter manex

    (@manex)

    Thank you for your answer, bcworkz.

    If I’ve understood correctly what I should do is the following:

    1. Add an action for the moment in which the post is published and open the pop-up in the registered function add_action( 'publish_post','pop-up_opener');
    2. As the post has already been published the content could be accessed from the pop-up window. Would something like this work? window.parent.getElementById("content").innerHTML;
    Moderator bcworkz

    (@bcworkz)

    You have the right idea, but I don’t think the publish post action is the right place to open a popup. PHP cannot actually open popups, it can only inject javascript which opens popups. I doubt injecting script from ‘publish_post’ would work, though it wouldn’t hurt to try πŸ™‚

    The proper approach is to enqueue javascript to open the popup on the post edit screen upon some event, since publishing the post simply reloads the same page with a “published” message. It sounds like you’ve already achieved this to some extent. Your proposed javascript is correct, but I suspect it’s not working or we would not be here.

    I’m not completely sure, but I don’t think following a link opens a true popup as far as javascript is concerned. It may look like one, but the “parent” reference is invalid. For the “parent” reference to work, I think you need to open a window with javascript, such as with window.open(). Thus instead of wrapping the button image with an anchor tag, you should assign an onclick action to the img tag that does the window.open(). Then the parent reference should work.

    You could instead open the popup when the page reloads after publish, which is closer to your most recent proposal. Then the content can come from the server instead of from the parent reference, though that should work as well.

    If following a link is really necessary, you would probably need to pass the content as an URL parameter. You could run into maximum URL length issues this way. And you still need javascript to get the current content and append it to the href attribute.

    There are really a number of ways to deal with this. If you don’t mind describing your intended user experience, I could suggest an approach that I think would work well.

    Thread Starter manex

    (@manex)

    Thank you for your guidance, it was realy useful. As you said, once a draft is saved the content is accesible from the popup. FYI, this is how I accomplished it:

    1. To create the popup: Define this function for the action media_buttons to add the image as a button to media buttons.
      function oi_itzultzaile_botoia_gehitu()
      	{
      				$urla='"'.get_bloginfo("wpurl").'/wp-content/plugins/ohar-itzultzailea/itzulpen-orria.php"';
      				$izena='"'.__("Ohar Itzultzailea","ohar-itzultzailea").'"';
      
      				echo "<img src='".get_bloginfo("wpurl")."/wp-content/plugins/ohar-itzultzailea/irudiak/itzuli-botoia.png'
      					class='thickbox'
      					alt='Itzulpena abian jartzeko erabiltzen den botoia irudikatzen duen irudia'
      					onclick='leihoaIreki($urla,$izena)' style='cursor: pointer;'/>";
      
      	}
      
      	add_action('media_buttons', 'oi_itzultzaile_botoia_gehitu',20);

      Then, in the .js file with the functions define this function to open the popup:

      function leihoaIreki(urla,izena)
      {
      	window.open(urla,izena,"width=640,height=500");
      }

    2. In order to get the content of the editor from the popup:
      var aux=window.opener.document.getElementById("content").innerHTML;

    With the code above I solved the problem. There is one issue, though, the pop-up does not follow the standard appearance of WordPress(in the previous version with the anchor it followed the standard appearance). I’ve tried a few things but I could not accomplish it, do you know how to do it?

    Anyway, once again, thank you very much! πŸ˜€

    Thread Starter manex

    (@manex)

    Moderator bcworkz

    (@bcworkz)

    Sorry, I’m not sure if achieving the exact same look is possible, you might be able to get close though. The DOM structure of each approach is completely different. I believe one is content in an iframe, so script is still running on the original page and can trigger effects like darkening the background.

    window.open() creates an entirely new, independent window. Script running on it has no control over the parent. You can of course manage the look of the new window by suppressing some window chrome and using CSS. Certain window chrome can be difficult to suppress in some browsers.

    Effects on the parent page could be initiated just before calling window.open() then canceled when the window regains focus.

    Thread Starter manex

    (@manex)

    Ok,thank you very much again. I will give it a try, πŸ˜€

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

The topic ‘Get post editor content from pop-up’ is closed to new replies.