0

Consider me newbie to web development and wordpress.

I am developing a plugin that randomly changes text color of elements. It is working good but its pretty much static right now. Now i want add this function for other elements too like post title.

How can i control conditions in a javascript .js file from options in an admin menu form. Like say I want to add that feature for post titles too so user will have a form with 2 checkboxes, one for blog title and other post title.

I have form ready with checkboxes, script working for title, just have to pass these checkbox values to that js file.

Thanks in advance.

Javascript file codes :

jQuery(document).ready(function($){

var $ttl = '.site-title a';

var rainbow = function(){
    var $rrr = Math.round(((Math.random()*200)));
    var $ggg = Math.round(((Math.random()*200)));
    var $bbb = Math.round(((Math.random()*200)));
    var $r = "'rgb(" + $rrr + ", " + $ggg + ", " + $bbb + ")'";
    $($ttl).animate({color: $r},"slow");
}

$($ttl).mouseenter(
    function(){
        rainbow()
    }
);
});

My plugin php file :

if ( ! function_exists( 'rainbow_title_script' ) ) {
function rainbow_title_script() {
    wp_enqueue_script(
        'rainbow_title_script',
        plugins_url( '/rainbow_title.js' , __FILE__ ),
        array( 'jquery-ui-core', 'jquery-color' )
        );
} // function ends
} // if condition ends

add_action('wp_enqueue_scripts','rainbow_title_script') 
4

1 回答 1

1

要创建选项页面,请参阅http://codex.wordpress.org/Creating_Options_Pages

http://wp.tutsplus.com/tutorials/theme-development/the-complete-guide-to-the-wordpress-settings-api-part-1/也提供了一个非常好的见解

编辑 请考虑:http ://wakeusup.com/2011/11/how-to-create-plugin-options-page-in-wordpress/

我在插件中使用了这种方法。您可以像这样设置您的设置:

if(!class_exists('cnp_plugin_options')):
    // DEFINE PLUGIN ID
    define('CNPPLUGINOPTIONS_ID', 'cnppluginoptions');
    // DEFINE PLUGIN NICK
    define('CNPPLUGINOPTIONS_NICK', 'CNP options');

    class cnp_plugin_options
    {
        /** function/method
        * Usage: hooking the plugin options/settings
        * Arg(0): null
        * Return: void
        */
        public static function register()
        {
            register_setting(CNPPLUGINOPTIONS_ID.'_options', 'cnp_builder');
            register_setting(CNPPLUGINOPTIONS_ID.'_options', 'cnp_author_restriction');
            register_setting(CNPPLUGINOPTIONS_ID.'_options', 'cnp_plugin_link');
            register_setting(CNPPLUGINOPTIONS_ID.'_options', 'cnp_notallowed');
        }
        /** function/method
        * Usage: hooking (registering) the plugin menu
        * Arg(0): null
        * Return: void
        */
        public static function menu()
        {
            // Create menu tab
            add_options_page(CNPPLUGINOPTIONS_NICK.' Plugin Options', CNPPLUGINOPTIONS_NICK, 'manage_options', CNPPLUGINOPTIONS_ID.'_options', array('cnp_plugin_options', 'options_page'));
        }
        /** function/method
        * Usage: show options/settings form page
        * Arg(0): null
        * Return: void
        */
        public static function options_page()
        {
            if (!current_user_can('manage_options'))
            {
                wp_die( __('You do not have sufficient permissions to access this page.') );
            }

            $plugin_id = CNPPLUGINOPTIONS_ID;
            // display options page
            include(plugin_dir_path(__FILE__).'/options.php');
        }
    if(is_admin()){
        add_action('admin_init', array('cnp_plugin_options', 'register'));
        add_action('admin_menu', array('cnp_plugin_options', 'menu'));
    }
endif;

现在您可以获得如下设置:

public static function cnp_notallowed(){
    if(get_option('cnp_notallowed')){
        return true;
    }else{
        return false;
    }
}
于 2013-10-29T15:20:34.140 回答