Question

To add an options page I followed the documentation for Creating Options Pages.

But I can't integrate any custom option of my plugin. Suppose in my plugin there is a shortcode and, in a section, there is a background which can be changed by custom coding. e.g ;

extract( shortcode_atts( array(
    'category' => '',
    'background'=> '#333',
    'border-width' => '10px'

), $atts, 'photo_gallery' ) );

$q = new WP_Query(
    array('photo_gallery_cat' => $category, 'posts_per_page' => -1, 'post_type' => 'photo-items')
    );


$list = '<section border="$border-width" style="background-color:'.$background.'"><ul id="gallery"><li style="background-color: '.$background.'" id="fullPreview"></li>';
while($q->have_posts()) : $q->the_post();

In the shortcode, I can use background="#aaa" to change the background color. With border-width also. But I want to create a option to change this by option page. How to integrate this in option page?

Also I need to use active functions options. e.g this:

function active_least_gallery() {?>

<script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery('#gallery').least({
            'random': true,
            'lazyload': false,
            'scrollToGallery': false
        });
    });
</script>

How to use this true/false options from the option page?

Was it helpful?

Solution

You have to pull the option with get_option('new_option_name'). In your shortcode:

// #aaa is the default, if nothing is saved yet
$background = get_option( 'new_option_name', '#aaa' ); 

And for the jQuery gallery:

$scroll = get_option( 'some_other_option', false );
<script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery('#gallery').least({
            'scrollToGallery': <?php echo $scroll; ?>
        });
    });
</script>

Or use wp_localize_script if you're enqueuing the script.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top