admin管理员组文章数量:1332352
I'm trying to make my first plugin, but when I try wp_enqueue_script
the script file, I get errors about the ID
, post_title
, post_name
, even though its a single page (i.e. not posts kind of page).
// enqueue scripts
function myplugin_enqueue_scripts( $hook ) {
if ( !is_page('my-page')) { return; }
// define script url
$script_url = plugins_url( 'public/myplugin.js', __FILE__ );
// enqueue script
// Below line cause the error
wp_enqueue_script('myplugin', $script_url);
}
add_action( 'wp_enqueue_scripts', 'myplugin_enqueue_scripts', 1);
I get this error message:
[23-Jun-2020 21:59:56 UTC] PHP Notice: Trying to get property 'ID' of non-object in /usr/www/users/sexerxm/example.server/wp-includes/class-wp-query.php on line 3994
[23-Jun-2020 21:59:56 UTC] PHP Notice: Trying to get property 'post_title' of non-object in /usr/www/users/sexerxm/example.server/wp-includes/class-wp-query.php on line 3996
[23-Jun-2020 21:59:56 UTC] PHP Notice: Trying to get property 'post_name' of non-object in /usr/www/users/sexerxm/example.server/wp-includes/class-wp-query.php on line 3998
Within myplugin.js
is one line:
console.log("Please work");
I also tried the template_redirect
hook but no difference. Why is this error being generated?
I'm trying to make my first plugin, but when I try wp_enqueue_script
the script file, I get errors about the ID
, post_title
, post_name
, even though its a single page (i.e. not posts kind of page).
// enqueue scripts
function myplugin_enqueue_scripts( $hook ) {
if ( !is_page('my-page')) { return; }
// define script url
$script_url = plugins_url( 'public/myplugin.js', __FILE__ );
// enqueue script
// Below line cause the error
wp_enqueue_script('myplugin', $script_url);
}
add_action( 'wp_enqueue_scripts', 'myplugin_enqueue_scripts', 1);
I get this error message:
[23-Jun-2020 21:59:56 UTC] PHP Notice: Trying to get property 'ID' of non-object in /usr/www/users/sexerxm/example.server/wp-includes/class-wp-query.php on line 3994
[23-Jun-2020 21:59:56 UTC] PHP Notice: Trying to get property 'post_title' of non-object in /usr/www/users/sexerxm/example.server/wp-includes/class-wp-query.php on line 3996
[23-Jun-2020 21:59:56 UTC] PHP Notice: Trying to get property 'post_name' of non-object in /usr/www/users/sexerxm/example.server/wp-includes/class-wp-query.php on line 3998
Within myplugin.js
is one line:
console.log("Please work");
I also tried the template_redirect
hook but no difference. Why is this error being generated?
1 Answer
Reset to default 0Issue 1
I was not seeing the embedded object in javascript because:
The error was, I thought plugins_url
always resolved to the plugins dir, it resolves to the dir of __FILE__
:
In my situation myplugin.js
and myplugin.php
are both in the same public/
dir, so this:
$script_url = plugins_url( 'public/myplugin.js', __FILE__ )
Was resolving as (notice the double public
):
http://server.example/wp-content/plugins/myplugin/public/public/myplugin.js
instead of
http://server.example/wp-content/plugins/myplugin/public/myplugin.js
Issue 2
I still see the error, however even with my plugin disabled, and managed to track it down to WooComerce by looking at the debug network tab and seeing the ajax request.
本文标签: wp enqueue scriptwpenqueuescript error with IDposttitlepostname of none object
版权声明:本文标题:wp enqueue script - wp_enqueue_script error with `ID`, `post_title`, `post_name` of none object 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742322183a2453010.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
plugins_url
function result? – Himad Commented Jun 23, 2020 at 22:23