如何在同一窗口中打开Wordpress"预览"链接?

cwd*_*cwd 5 html javascript wordpress jquery

我问如何在同一窗口中保存和预览"预览发布"按钮?在Wordpress Stack Exchange上,但这可能是Stack Overflow的一个更好的问题,因为它与编码更直接相关.

Wordpress有一个框,允许您保存,预览和发布您的博客帖子:

Picture.png http://img854.imageshack.us/img854/7986/picturek.png

"预览"按钮实际上是一个设为按钮的链接:

<a tabindex="4" id="post-preview" target="wp-preview"
href="/?p=67&amp;preview=true" class="preview button">Preview</a>
Run Code Online (Sandbox Code Playgroud)

我的问题是我似乎无法弄清楚如何在当前窗口中打开该链接.请注意该target="wp-preview"部分.我试图摆脱那个部分,但我认为可能有另一个函数绑定到该元素,因为我真的无法让它在当前的选项卡/窗口中打开,即使解除绑定并删除target属性.

我正在运行以下代码作为插件的一部分(您可以在下面看到有关如何将其作为插件运行的更多信息),但也可以将其复制并粘贴到Chrome或Firefox的控制台中以自行测试甚至修改Wordpress.请注意,在测试时,您需要使用jQuery而不是$在您自己的函数中,因为Wordpress使用该noconflict方法,但是下面的代码工作正常.

//select the node and cache the selection into a variable
var $node = jQuery('a.preview'); 

//add a 1px dotted outline to show we have the right element
$node.css('outline','1px dotted red'); 

//show current target
console.log($node.prop('target')); 
//show if anything is bound - nothing is for me ('undefined')
console.log($node.data('events')); 

//remove anything bound to it
$node.unbind(); 
//set target to _self (current window), just in case
$node.prop('target','_self'); 
//the remove the target attribute all together
$node.removeAttr('target'); 

//clone the node
var $clone = $node.clone(); 
//change the text to new
$clone.text('new'); 
//remove target from clone
$clone.removeAttr('target'); 
//unbind the clone
$clone.unbind(); 
//insert the clone after the original node
$node.after($clone); 

//show current target - now shows undefined for me
console.log($node.prop('target'));
//show if anything is bound - still 'undefined'
console.log($node.data('events'));
Run Code Online (Sandbox Code Playgroud)

这是您将代码用于主题或插件的方式:

// set up an action to set a function to run in the wp admin_footer
add_action('admin_footer','my_admin_footer_script',9999);
Run Code Online (Sandbox Code Playgroud)

这是添加javascript的函数:

//this function can then be used to add javascript code to the footer

function my_admin_footer_script(){ ?>
    <script type="text/javascript">
    jQuery(function($){
     (above js code here)
    });
    </script>


    <?php
}
Run Code Online (Sandbox Code Playgroud)

这是我最终得到的结果.如果我单击"测试"链接,它将在同一窗口中打开.如果单击"预览"链接,它仍会在新选项卡中打开.

Picture.png http://img832.imageshack.us/img832/4163/picturesf.png

ps:我正在使用你可能不了解jQuery的方法来检查绑定事件,我没有找到任何绑定,我相信Wordpress主要使用jQuery所以我不认为这会与另一个绑定事件处理程序

Kev*_*nis 3

你可以试试这个:

jQuery('.preview.button').click(function(e){
    window.location.href = this.href;
    return false;
});
Run Code Online (Sandbox Code Playgroud)

在 Chrome 检查器中工作。