Javascript 将插件构建到主题中

Javascript 将插件构建到主题中,javascript,php,ajax,wordpress,plugins,Javascript,Php,Ajax,Wordpress,Plugins,首先,我知道这是一个“坏”的做法,有几个不同的原因,但我不想浪费你的时间,这是我需要做的事情 使用一个相当简单的插件,我试着将它复制到functions.php并更新文件路径和依赖项,但我遇到了一些麻烦 Plugin.php文件如下所示:- $plugin_headers = get_file_data( __FILE__, array( 'Version' => 'Version', 'Name' => 'Plugin Name' ) ); /** * We store ou

首先,我知道这是一个“坏”的做法,有几个不同的原因,但我不想浪费你的时间,这是我需要做的事情

使用一个相当简单的插件,我试着将它复制到functions.php并更新文件路径和依赖项,但我遇到了一些麻烦

Plugin.php文件如下所示:-

$plugin_headers = get_file_data( __FILE__, array( 'Version' => 'Version', 'Name' => 'Plugin Name' ) );


/**
 * We store our plugin data in the following global array.
 * $my_unique_name with your unique name
 */
global $my_unique_name;
$my_unique_name = array();
$my_unique_name['version_key'] = strtolower( str_replace( ' ', '_', $plugin_headers['Name'] ) ) . '_version';
$my_unique_name['version_value'] = $plugin_headers['Version'];


/**
 * When the user activates the plugin we add the version number to the
 * options table as "my_plugin_name_version" only if this is a newer version.
 */
function inline_comments_acitvation(){

    global $my_unique_name;

    if ( get_option( $my_unique_name['version_key'] ) && get_option( $my_unique_name['version_key'] ) > $my_unique_name['version_value'] )
        return;

    update_option( $my_unique_name['version_key'], $my_unique_name['version_value'] );

}
register_activation_hook( __FILE__, 'inline_comments_acitvation' );


/**
 * Delete our version number from the database when the plugin is activated.
 */
function inline_comments_deactivate(){
    global $my_unique_name;
    delete_option( $my_unique_name['version_key'] );
}
register_deactivation_hook( __FILE__, 'inline_comments_deactivate' );


if ( is_admin() )
    require_once plugin_dir_path( __FILE__ ) . 'admin/admin-tags.php';

/**
 * Theme only functions
 */
require_once plugin_dir_path( __FILE__ ) . 'inc/template-tags.php';


function inline_comments_enqueue_scripts(){

    $plugin_headers = get_file_data( __FILE__, array( 'Version' => 'Version', 'Name' => 'Original Plugin Name' ) );
    $clean_name = strtolower( str_replace( ' ', '-', $plugin_headers['Name'] ) );

    wp_register_style( $clean_name . '-style', plugin_dir_url( __FILE__ ) . 'inc/css/style.css' );
    wp_register_script( 'textarea_auto_expand-script', plugin_dir_url( __FILE__ ) . 'vendor/textarea-auto-expand/jquery.textarea_auto_expand.js' );
    wp_register_script( $clean_name . '-script', plugin_dir_url( __FILE__ ) . 'inc/js/script.js', array('jquery', 'textarea_auto_expand-script') );
}

add_action('wp_enqueue_scripts', 'inline_comments_enqueue_scripts', 2);
在将插件移动到主题文件夹后,我做了以下工作:删除了无意义的部分,并在functions.php中加载了main script.js(它加载)和css,就像这样(即更改了结构并将脚本移动到相关文件夹中)

好的,这样就可以加载脚本了

该插件的主模板文件(包含函数和ajax调用)位于template-tags.php中,但我不知道如何正确加载该文件

require_once plugin_dir_path( __FILE__ ) . 'inc/template-tags.php';
我尝试过将其复制/粘贴到functions.php中,但它似乎也不起作用

模板-tags.php:

<?php

/**
 * @todo Ajax crawling support -- https://developers.google.com/webmasters/ajax-crawling/docs/getting-started
 * @todo https://developers.google.com/webmasters/ajax-crawling/
 */


/**
 * Perform the following actions/filters when plugins are loaded
 *
 * @since 0.1-alpha
 */
function inline_comments_loaded(){
    add_action( 'wp_ajax_inline_comments_add_comment', 'inline_comments_add_comment' );
    add_action( 'wp_ajax_nopriv_inline_comments_add_comment', 'inline_comments_add_comment' );
    add_action( 'wp_ajax_nopriv_inline_comments_load_template', 'inline_comments_load_template' );
    add_action( 'wp_ajax_inline_comments_load_template', 'inline_comments_load_template' );
    add_filter( 'template_redirect', 'inline_comments_template_redirect' );
}
add_action('plugins_loaded', 'inline_comments_loaded');


/**
 * Load our JavaScript and Stylesheet on single page only
 *
 * @since 0.1-alpha
 */
function inline_comments_template_redirect() {
    if ( is_singular() || is_page() ) {
        add_action( 'wp_enqueue_scripts', 'inline_comments_scripts');
        add_action( 'wp_head', 'inline_comments_head');
    }
}


/**
 * Load our JavaScript and Stylesheet, we include the login-register script only if it is installed.
 *
 * @uses wp_enqueue_script()
 * @uses wp_enqueue_style()
 *
 * @since 0.1-alpha
 */
function inline_comments_scripts(){
    wp_enqueue_script( 'inline-ajax-comments-script' );
    wp_enqueue_style( 'inline-ajax-comments-style' );
}


/**
 * Print our AJAX URL
 *
 * @since 0.1-alpha
 */
function inline_comments_head(){
    print '<script type="text/javascript"> var ajaxurl = "'. admin_url("admin-ajax.php") .'";</script>';
    print '<style type="text/css">'.get_option('additional_styling').'</style>';
}


/**
 * Inserts a comment for the current post if the user is logged in.
 *
 * @since 0.1-alpha
 * @uses check_ajax_referer()
 * @uses is_user_logged_in()
 * @uses wp_insert_comment()
 * @uses wp_get_current_user()
 * @uses current_time()
 * @uses wp_kses()
 * @uses get_option()
 */
function inline_comments_add_comment(){

    check_ajax_referer('inline_comments_nonce', 'security');

    $comment = trim(
            wp_kses( $_POST['comment'],
            array(
                'a' => array(
                    'href'  => array(),
                    'title' => array()
                ),
                'br'         => array(),
                'em'         => array(),
                'strong'     => array(),
                'blockquote' => array(),
                'code'       => array()
            )
        )
    );

    if ( empty( $comment ) ) die();

    if ( get_option('comment_registration') == 1 && ! is_user_logged_in() ) die();

    $data = array(
        'comment_post_ID' => (int)$_POST['post_id'],
        'comment_content' => $comment,
        'comment_type' => '',
        'comment_parent' => 0,
        'comment_author_IP' => $_SERVER['REMOTE_ADDR'],
        'comment_agent' => $_SERVER['HTTP_USER_AGENT'],
        'comment_date' => current_time('mysql'),
        'comment_approved' => 1
    );

    if ( is_user_logged_in() ){
        $current_user = wp_get_current_user();

        $author_email = $current_user->user_email;
        $author_url = $current_user->user_url;
        $author_name = $current_user->user_nicename;

        $data['user_id'] = $current_user->ID;
    } else {
        $author_email = empty( $_POST['user_email'] ) ? null : esc_attr( $_POST['user_email'] );
        $author_url = empty( $_POST['user_url'] ) ? null : esc_url( $_POST['user_url'], array('http','https') );
        $author_name = empty( $_POST['user_name'] ) ? null : esc_attr( $_POST['user_name'] );
    }

    $data['comment_author'] = $author_name;
    $data['comment_author_email'] = $author_email;
    $data['comment_author_url'] = $author_url;

    // ck - catch the new comment id for updating comment meta
    $comment_id = wp_insert_comment( $data );

    // ck - now add the para-id to the comment meta
    add_comment_meta( $comment_id, 'para_id' , $_POST['para_id'] ); 

    die();
}


/**
 * Load comments and comment form
 *
 * @since 0.1-alpha
 */
function inline_comments_load_template(){

    check_ajax_referer('inline_comments_nonce', 'security');

    $comments = get_comments( array(
        'post_id' => (int)$_POST['post_id'],
        'number'  => 100,
        'status'  => 'approve',
        'order'   => 'ASC'
    ) );

    ?>
    <div class="inline-comments-container" id="comments_target">
        <?php if ( $comments ) : foreach( $comments as $comment) : ?>
            <?php

            // ck get the paragraph id from the comment meta
            $para_id = get_comment_meta( $comment->comment_ID, 'para_id', true );

            $user = new WP_User( $comment->user_id );
            $class = null;
            if ( ! empty( $user->roles ) && is_array( $user->roles ) ) {
                foreach ( $user->roles as $role ){
                    $class = $role;
                }
            } else {
                $class = 'annon';
            }

            // ck -added data-comment-para-id to div
            ?>
            <div class="orphan-comment comment-para-id-<?php echo $para_id ?> inline-comments-content inline-comments-<?php echo $class; ?>" id="comment-<?php echo $comment->comment_ID; ?>">
                <div class="inline-comments-p">
                    <?php inline_comments_profile_pic( $comment->comment_author_email ); ?>
                    <?php print $comment->comment_content; ?><br />
                    <time class="meta">
                        <strong><?php $user = get_user_by('login', $comment->comment_author ); if ( ! empty( $user->user_url ) ) : ?><a href="<?php print $user->user_url; ?>" target="_blank"><?php print $comment->comment_author; ?></a><?php else : ?><?php print $comment->comment_author; ?><?php endif; ?></strong>
                        <a href="<?php echo get_permalink( $comment->comment_post_ID); ?>#<?php echo $comment->comment_ID; ?>" class="inline-comments-time-handle" data-comment_id="<?php echo $comment->comment_ID; ?>"><?php print human_time_diff( strtotime( $comment->comment_date ), current_time('timestamp') ); ?> ago.</a>
                    </time>
                </div>
            </div>
        <?php endforeach; endif; ?>
    </div>
    <?php die();
}


/**
 * Determine the profile pic for a user, either the FB pic or
 * the gravatar pic. If no ID is passed uses the current logged
 * in user.
 *
 * @uses get_user_meta()
 * @uses get_avatar();
 */
function inline_comments_profile_pic( $id_or_email=null, $email=null ){

    if ( is_null( $id_or_email ) ) {
        global $current_user;
        get_currentuserinfo();
        $id_or_email = $current_user->ID;
    }

    $html = get_avatar( $id_or_email, 32 );

    print '<span class="inline-comments-profile-pic-container">' . $html . '</span>';
}


function inline_comments_tempalte( $file ){
    return plugin_dir_path( dirname( __FILE__ ) ) . 'templates/comments.php';
}
add_filter('comments_template', 'inline_comments_tempalte');

我注意到您已将/css和/js目录从/inc中移出,它们似乎是原来所在的位置

因此,请尝试:

require_once plugin_dir_path( __FILE__ ) . 'template-tags.php';

plugin_dir_path-但是文件现在在我的主题文件夹中。尝试了这个,但没有加载。控制台显示“uncaughtreferenceerror:ajaxurl未定义”,因此它正在加载脚本,而不是template-tags.php文件。用模板标签文件编辑我的问题。等等,那么你以前没有得到ajaxurl,现在得到了?或者您在更改之前和之后都出现了错误?更改之前和之后。require\u once应该在您的php错误日志中生成一个错误。错误说明了什么?它可能指示它试图定位文件的位置。
PHP致命错误:require_once()[]:无法打开required'/Applications/MAMP/htdocs/wp content/themes/裸wordpress master/options.PHP'(include_path='。:/Applications/MAMP/bin/PHP/php5.3.6/lib/PHP')在第74行的/Applications/MAMP/htdocs/wp content/themes/裸wordpress master/functions.php中
我以前见过这个问题……你确实见过,好先生!
require_once plugin_dir_path( __FILE__ ) . 'template-tags.php';