我正在尝试开发一个插件,在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代码时,它可以工作,但当我试图将它转换成一个插件时,它不工作。它有什么问题?
发布于 2013-05-23 05:54:26
尝试:改为add_action( 'init', 'wp_snowfall_plugins_loaded' );
发布于 2013-07-06 06:59:17
您错误地使用了jQuery。
然后,使用以下noConflict模式启动jQuery命令:
jQuery(document).ready(function($) {
$.fn.snow();
});我使用Snowfall version 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 Demo Class,它会升级你的基础插件。使用常量不是一种好的做法...
另外,在WordPress Answers上研究一下plugin-development标签,有很多有用的信息;)
https://stackoverflow.com/questions/16699960
复制相似问题