简体   繁体   English

Wordpress插件未加载Javascript文件

[英]Wordpress Plugin Not Loading Javascript File

I am creating/learning about wordpress plugins and I have created one that adds a meta box to the edit post page. 我正在创建/学习有关wordpress插件的信息,并且已经创建了一个将meta框添加到编辑帖子页面的插件。 However, I can't seem to get the plugin to load my .js file. 但是,我似乎无法让插件加载我的.js文件。

<?php
   /*
   Plugin Name: Adams The The Plugin
   Plugin URI: http://adamthings.com
   Description: Plugin attempt...
   Version: 1.0
   Author: Adam
   Author URI: http://adamthings.com
   License: GPL2
   */

   function my_scripts_method() {
        wp_enqueue_script('the_js', plugins_url('/AdamsTheThePlugin.js',__FILE__) );
    }

    add_action( 'wp_enqueue_scripts', 'my_scripts_method' );

    add_action( 'add_meta_boxes', 'cd_meta_box_add' );

    function cd_meta_box_add()
    {
        add_meta_box( 'my-meta-box-id', 'Adams The The Plugin', 'cd_meta_box_cb', 'post', 'normal', 'high' );
    }

    function cd_meta_box_cb( $post )
    {
        $values = get_post_custom( $post->ID );
        $postid = get_the_ID();
        $content = get_post_field('post_content', $postid);

        wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>

    <p>
        <label for="my_meta_box_text" id="adamsPostContent">Post Content</label>
        <textarea name="my_meta_box_text" id="my_meta_box_text"><?php echo $content; ?></textarea>
    </p>


<?php   
    }
?>

Then my JavaScript file is in the same directory as the plugin is and looks like: 然后,我的JavaScript文件与插件位于同一目录中,如下所示:

jQuery(document).ready(function () {
    jQuery("#adamsPostContent").css('color', 'red');
});

From other posts it seems like I am doing it correctly. 从其他帖子看来,我做得正确。 Thoughts? 思考?

I would expect the label adamsPostContent to turn red on load. 我希望标签adamsPostContent在加载时变为红色。

It looks like you are building and admin-side plugin, but you are enqueueing the JS file in the front-end. 看起来您正在构建和管理端插件,但是您正在排队JS文件的前端。 You need to use the admin_enqueue_scripts action hook, so your call will look like this: 您需要使用admin_enqueue_scripts操作挂钩,因此您的调用将如下所示:

add_action( 'admin_enqueue_scripts', 'my_scripts_method' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM