0

我正在尝试开发一个在 Wordpress 网站上创建降雪效果的插件。我的代码如下:

<?php
/*
Plugin Name: tuhin 
Plugin URI: no plugin uri
Description: This plugin will add a Simple & lightweight responsive slider.
Author: tuhin
Author URI: no author uri
Version: 1.0
*/
/* Launch the plugin. */
add_action( 'plugins_loaded', 'wp_snowfall_plugins_loaded' );

/* Initializes the plugin and it's features. */
function wp_snowfall_plugins_loaded() {
    /* Set constant path to the members plugin directory. */
    define( 'WP_SNOWFALL_DIR', plugin_dir_path( __FILE__ ) );

    /* Set constant path to the members plugin directory. */
    define( 'WP_SNOWFALL_URL', plugin_dir_url( __FILE__ ) );

    /* Loads the snowfall. */
    add_action('wp_head', 'wp_snowfall_source');
    add_action('wp_head', 'wp_snowfall_effect');
}


function wp_snowfall_source() { 
    wp_enqueue_script( 'jquery.js' );
    wp_enqueue_script( 'snowfall', WP_SNOWFALL_URL.'snow.min.jquery.js' );
}

function wp_snowfall_effect() { ?>
<script type="text/javascript">
$(document).ready( function(){
    $.fn.snow();
});
</script>
<?php
}
?>

当我在我的主题中复制这个 jQuery 代码时它可以工作,但是当我尝试将它转换为插件时它不起作用。它出什么问题了?

4

2 回答 2

0

您错误地使用了 jQuery。

然后,使用以下noConflict模式启动您的 jQuery 命令:

jQuery(document).ready(function($) {   
    $.fn.snow();
});

我使用Snowfall 1.4 版进行了测试,这是工作代码(注意jquery作为 的依赖项排队snowfall)。

add_action( 'plugins_loaded', 'wp_snowfall_plugins_loaded' );

/* Initializes the plugin and it's features. */
function wp_snowfall_plugins_loaded() {
    /* Set constant path to the members plugin directory. */
    define( 'WP_SNOWFALL_DIR', plugin_dir_path( __FILE__ ) );

    /* Set constant path to the members plugin directory. */
    define( 'WP_SNOWFALL_URL', plugin_dir_url( __FILE__ ) );

    /* Loads the snowfall. */
    add_action('wp_head', 'wp_snowfall_source');

    /* Startup the snowfall. */
    add_action('wp_footer', 'wp_snowfall_effect', 999 );
}


function wp_snowfall_source() { 
    wp_enqueue_script( 'snowfall', WP_SNOWFALL_URL.'snowfall.min.jquery.js', array('jquery') );     
}

function wp_snowfall_effect() { 
    ?>
    <script type="text/javascript">
    jQuery(document).ready(function($) {   
        $(document).snowfall();
    });
    </script>
    <?php
}

查看插件演示类,它将升级您的基础插件。使用常量不是一个好习惯......

另外,研究plugin-developmentWordPress Answers 上的标签,有很多有用的信息;)

于 2013-07-05T22:59:16.707 回答
0

尝试:add_action( 'init', 'wp_snowfall_plugins_loaded' );改为

于 2013-05-22T21:54:26.453 回答