jQuery冲突! 在Word编辑的POST EDIT页面上显示白屏

我有一个小的jQuery脚本从其他文件导入值,并在WordPress POST页面中插入此值作为自定义元标记。

当我创建一个新post时,表格显示在post内容区域下方,并且每件事情都是100%工作。

问题是如果我按下“编辑后”按钮/链接然后它加载后编辑页面,但没有显示任何内容。 只是一个白色的屏幕加载,所以我不能编辑这篇文章。

这是我的脚本:

    $(document).ready(function() { $('.button').click(function() { var val = $('#file').val(); $.get('import.php', {file: val}, function(data) { result = $.parseJSON(data); $("input[name='nick_name']").val(result.name); $("input[name='work_city']").val(result.location); }); }); });   

我还尝试添加此脚本(在jquery.min.js之后):

  $.noConflict(true);  

然后编辑post页面工作,但我无法使用我的自定义表单/按钮。

我的问题是:如何在不与WordPress编辑post页面发生任何冲突的情况下加载此脚本?

如果我删除:

   

从我的脚本然后所有页面加载确定,但然后我的自定义表单/按钮不起作用。

最新更新:我正在工作:D:D:D只是测试我应该在何时何地加载此脚本以成功获得我的结果。 我开始编辑/wp-admin/admin-header.php(我知道它不建议编辑CORE文件,但我只是为debuggig做!)首先,我在开放之前插入了我在第1行使用的jQuery脚本

   

然后我去POST新页面,我可以看到一些不同的字体大小,但我想要的function就在那里,我可以使用我的按钮。 所以我打开了编辑POST页面,我可以看到编辑页面! 现在编辑页面也在工作。 最后我想出如果我LINE 59 in /wp-admin/admin-header.php开始插入这个jQuery脚本,那么每件事都可以100%工作 :D设计CSS看起来不错,function就在那里!

现在我需要帮助将此脚本插入/粘贴到admin-header.php中的LINE 59。如何使用functions.php执行此操作?

首先,编辑wordpress核心似乎是一件坏事。 但是如果你想这样做,请不要再包含jQuery。 并且不要使用$符号,请参阅此示例:

 jQuery(".SomeClass").click(function(){ console.log("Some Text"); }); //Therefore you could do something like this   

如何添加脚本WordPress方式:

 add_action( 'wp_enqueue_scripts', 'pjso_my_scripts' ); function pjso_my_scripts() { $handle = 'script_name'; $src = '/path/to/script.js'; $deps = array( 'jquery', ); // add any other dependencies to the array, using their $handle $ver = 'x.x'; // if you leave this NULL, // then it'll use WordPress's version number $in_footer = true; // if you want it to load in the page footer wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ); } 

要仅在后端的“编辑post”屏幕上执行此操作,您可以使用admin_enqueue_scripts钩子与get_current_screen()结合使用:

 add_action( 'admin_enqueue_scripts', 'pjso_my_scripts' ); function pjso_my_scripts() { $screen = get_current_screen(); if( 'post' != $screen->base || 'edit' != $screen->parent_base ) { return; // bail out if we're not on an Edit Post screen } $handle = 'script_name'; $src = '/path/to/script.js'; $deps = array( 'jquery', ); // add any other dependencies to the array, using their $handle $ver = 'x.x'; // if you leave this NULL, // then it'll use WordPress's version number $in_footer = true; // if you want it to load in the page footer wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ); } 

应该工作,虽然我还没有测试过。

此代码可以放在主题的functions.php文件中,或者您可以编写自定义插件,以便它可以在任何未来的主题更改中保留。

参考

  • wp_enqueue_script() – 注意WP自动加载的脚本列表,其中包括jQuery
  • get_current_screen()