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:
- 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');
- 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;
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:
- 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");
}
- 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! π
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, π