wordpress theme customizer dropdown - php

I am trying to create a select box/dropdown menu in the Wordpress theme customization area where the options are to be extracted from a column called "alias" in a table named "wp_revslider_sliders" from my Wordpress DB.
I have already created the section, and the basic setting and control for the drop down menu (see Fig. 1 below), but being a novice in this area, I can't figure out how to query the Wordpress DB, extract the results from the "Alias" column of my "wp_revslider_sliders" table and insert them output in to the "choices array" below
Fig. 1
$wp_customize->add_control(
'select_revslider',
array(
'type' => 'select',
'label' => 'Please Select a Slider:',
'section' => 'example_section_one',
'choices' => array(
'wordpress' => 'WordPress',
),
)
);
Fig. 2
function example_customizer( $wp_customize ) {
$wp_customize->add_section(
'example_section_one',
array(
'title' => 'Example Settings',
'description' => 'This is a settings section.',
'priority' => 35,
)
);
$wp_customize->add_setting(
'select_revslider',
array(
'default' => 'wordpress',
)
);
$wp_customize->add_control(
'select_revslider',
array(
'type' => 'select',
'label' => 'Please Select a Slider:',
'section' => 'example_section_one',
'choices' => array(
'wordpress' => 'WordPress',
),
)
);

Within your function.php file crate function like this:
function alias_from_wp_revslider_sliders() {
$sql = "SELECT aliasname FROM wp_revslider_sliders";
$result = $conn->query($sql);
$alias_list = array();
foreach($alias_list as $alias) {
$alias_list[$alias->aliasname] = $alias->aliasname;
}
return $alias_list;
}
and than when you add the control instead of choices call the function we created inside functions.php
$wp_customize->add_control(
'select_revslider',
array(
'type' => 'select',
'label' => 'Please Select a Slider:',
'section' => 'example_section_one',
'choices' => function alias_from_wp_revslider_sliders(),
)

Related

Add 3 checkout select fields as a composite date field in Woocommerce 3

I'm looking to add three custom date fields using select. So the first one would be the day, second would be month and the third would be the year. However, I'm not sure how to add a code without manually entering all years pluss to have three fields count as one.
I have a code I used to add a custom field 'gender' any way to something similar for a dob select fields described above.
add_action('woocommerce_before_checkout_billing_form', 'wps_add_select_checkout_field');
function wps_add_select_checkout_field( $checkout ) {
woocommerce_form_field( 'apgen', array(
'type' => 'select',
'class' => array( 'ap-drop' ),
'label' => __( 'Gender' ),
'options' => array(
'blank' => __( 'Select Gender', 'ap' ),
'male' => __( 'Male', 'ap' ),
'Female' => __( 'Female', 'ap' ),
'non-binary' => __( 'Non-binary', 'ap' )
)
),
$checkout->get_value( 'apgen' ));
}
Woocommerce has now a "date" field type with a very specific behavior, that has inside its field, 3 selectable number field type (days, month and year) that you can select individually, with a showable date picker (see screenshots below):
add_filter('woocommerce_checkout_fields', 'wps_add_checkout_field');
function wps_add_checkout_field( $fields ) {
// Select field (Gender)
$fields['billing']['billing_gender'] = array(
'type' => 'select',
'class' => array( 'form-row-wide' ),
'label' => __( 'Gender' ),
'required' => true,
'priority' => 3,
'options' => array(
'' => __( 'Select Gender', 'ap' ),
'male' => __( 'Male', 'ap' ),
'Female' => __( 'Female', 'ap' ),
'non-binary' => __( 'Non-binary', 'ap' )
),
);
// Date field (with 3 number fields with a datepicker)
$fields['billing']['billing_date'] = array(
'type' => 'date',
'class' => array( 'form-row-wide' ),
'label' => __( 'Date' ),
'required' => true,
'priority' => 3,
);
return $fields;
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
You will get something like:
Rolling in the date field:
Selecting days and using to increase or decrease days:
Selecting months and increasing value (or typing a value):
Selecting years and increasing value (or typing a value):
Making appear the date picker and select a date:
Other similar hooks that can be used:
add_filter('woocommerce_billing_fields', 'wps_add_date_type_checkout_field');
function wps_add_date_type_checkout_field( $fields ) {
// Date field (with 3 number fields with a datepicker)
$fields['billing_date'] = array(
'type' => 'date',
'class' => array( 'form-row-wide' ),
'label' => __( 'Date' ),
'priority' => 5,
);
return $fields;
}
Or
add_action('woocommerce_before_checkout_billing_form', 'wps_add_date_type_checkout_field');
function wps_add_date_type_checkout_field( $checkout ) {
woocommerce_form_field( 'billing_date', array(
'type' => 'date',
'class' => array( 'form-row-wide' ),
'label' => __( 'Gender' ),
), $checkout->get_value( 'billing_date' ));
}
Or before (or after) order notes:
add_action('woocommerce_before_order_notes', 'wps_add_date_type_checkout_field');
// add_action('woocommerce_after_order_notes', 'wps_add_date_type_checkout_field');
function wps_add_date_type_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field">';
woocommerce_form_field( 'billing_date', array(
'type' => 'date',
'class' => array('my-field-class form-row-wide'),
'label' => __('Date'),
), $checkout->get_value( 'billing_date' ));
echo '</div>';
}
Related docs: Customizing Woocommerce checkout fields using actions and filters

List posts in the wordpress theme customiser

Hey there. I am creating a Wordpress theme and was wondering if anyone knew of a way to list one of my custom post types in the theme customiser. I have a custom post type called "slideshow" that has custom meta boxes etc and is designed just for slideshows. I would like to be able to list these posts in a dropdown inside the customiser. Ideally ending up with them in the array like this...
'the_id' => 'Slideshow post title',
$wp_customize->add_setting(
'slideshow-homepage',
array(
'default' => 'none',
)
);
$wp_customize->add_control(
'slideshow-homepage',
array(
'type' => 'select',
'priority' => 3,
'label' => 'Slideshow',
'description' => '',
'section' => 'homepage',
'choices' => array(
'somehow' => 'somehow',
'list' => 'list',
'all' => 'all',
'custom' => 'custom',
'post' => 'post',
'types' => 'types',
'of' => 'of',
'type' => 'type',
'slideshow' => 'slideshow'
),
)
);
Many thanks guys and girls. Lewis
use array_reduce
$wp_customize->add_control(
'slideshow-homepage',
array(
'type' => 'select',
'priority' => 3,
'label' => 'Slideshow',
'description' => '',
'section' => 'homepage',
'choices' => array_reduce(
get_posts( 'post_type=slideshow&posts_per_page=-1' ),
function( $result, $item ) {
$result[$item->ID] = $item->post_title;
return $result;
}
),
)
);
To add an empty field as well:
$posts = array_reduce(
get_posts( 'post_type=slideshow&posts_per_page=-1' ),
function( $result, $item ) {
$result[$item->ID] = $item->post_title;
return $result;
}
);
$none = array('' => 'None');
$choices = $none + $posts;
$wp_customize->add_control('slideshow_control', array(
'label' => __('Choose Slideshow', 'themename'),
'section' => 'slideshow_option',
'settings' => 'slideshow_settings',
'type' => 'select',
'choices' => $choices
));

Getting Wordpress Meta Box values to appear on my posts?

I'm a newbie Wordpress developer & I can't figure out how to get the options chosen in my Custom Meta Boxes to populate on my posts. For example, this is how my admin panel looks when creating new posts:
http://i.stack.imgur.com/9RGz8.jpg
To give you an example of my code for this section that is in my meta.php file... It's:
//POST META BOXES
'post'=> array(
array(
'name' => 'Home Type',
'id' => PEXETO_META_PREFIX.'text',
'type' => 'select',
'options' => array( array( 'name'=>'1', 'id'=>'1' ),
array( 'name'=>'2', 'id'=>'2' ),
array( 'name'=>'3', 'id'=>'3' ),
array( 'name'=>'4', 'id'=>'4' ),
array( 'name'=>'5', 'id'=>'5' ) ),
'desc' => '...'
),
array(
'name' => 'Listing Status',
'id' => PEXETO_META_PREFIX.'text',
'type' => 'select',
'options' => array( array( 'name'=>'1', 'id'=>'1' ),
array( 'name'=>'2', 'id'=>'2' ),
array( 'name'=>'3', 'id'=>'3' ) ),
'desc' => '...'
),
array(
'name' => 'Lot Size',
'id' => PEXETO_META_PREFIX.'text',
'type' => 'text',
'desc' => '...'
),
What I'm trying to do is set it up so that each meta box will generate a two column row in my actual post. The left side would show the title of the box, and the right side would show the answer given by the user. An example of this is below:
http://i.stack.imgur.com/suJTq.jpg
Any help with this one would be greatly appreciated as I'm stuck...
Thanks.
You should look into get_post_meta()
Then something like this to echo those metas out in a table:
$metas = get_post_meta( get_the_ID() );
echo '<table>';
foreach( $metas as $key => $meta ) {
printf('<tr><th>%s</th><td>%s</td></tr>',
$key, $meta[0] );
}
echo '</table>';

select box in wordpress for meta field

I am registering a post in my wp-admin but I don't want editor etc, so adding some field. By the R & D I founded how to add text box and it's awesome but now I have to add a select box and the option value should be post title. I don't want to do this by plugin.
I added text field as:
$client_meta_box = array(
'id' => 'meta-client',
'title' => __('Client Options','mfn-opts'),
'page' => 'client',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'id' => 'post-link',
'type' => 'text',
'title' => __('Link', 'opts'),
'sub_desc' => __('Link to client`s site', 'opts'),
),
),
);
and I can add select box by just change the type as 'type' => 'select' but how did I get the post title value in option.
Using this to add meta box lile text, chackbox, selectoption.
$meta_boxes[] = array(
'id' => 'meta-client', // meta box id, unique per meta box
'title' => 'Client Options', // meta box title
'pages' => array('client'), // post types, accept custom post types as well,
//default is array('post'); optional
'priority' => 'high', // order of meta box: high (default), low; optional
'fields' => array(
array(
'label'=> 'Text Input',
'desc' => 'A description for the field.',
'id' => $prefix.'text',
'type' => 'text'
),
array(
'label'=> 'Textarea',
'desc' => 'A description for the field.',
'id' => $prefix.'textarea',
'type' => 'textarea'
),
array(
'label'=> 'Checkbox Input',
'desc' => 'A description for the field.',
'id' => $prefix.'checkbox',
'type' => 'checkbox'
),
array(
'label'=> 'Select Box',
'desc' => 'A description for the field.',
'id' => $prefix.'select',
'type' => 'select',
'options' => array(
'option1' => 'Optionone', // 'value'=>'label'
'option2' => 'Optiontwo',
'option3' => 'Optionthree'
)
)
);

How to have more than one "Theme Options" page with OptionTree?

OptionTree (GitHub) allows to create a "Theme Options" page for themes very simply.
How could I extend OptionTree in order to create a "Plugin Options" page for my plugin?
Thank you!
It's actually pretty simple. The following code will create a page under the settings page called Test Page. This is how OptionTree creates its own pages.
/**
* Hook to register admin pages
*/
add_action( 'init', 'register_options_pages' );
/**
* Registers all the required admin pages.
*/
function register_options_pages() {
// Only execute in admin & if OT is installed
if ( is_admin() && function_exists( 'ot_register_settings' ) ) {
// Register the pages
ot_register_settings(
array(
array(
'id' => 'custom_options',
'pages' => array(
array(
'id' => 'test_page',
'parent_slug' => 'options-general.php',
'page_title' => 'Test Page',
'menu_title' => 'Test Page',
'capability' => 'edit_theme_options',
'menu_slug' => 'test-page',
'icon_url' => null,
'position' => null,
'updated_message' => 'Test Page updated.',
'reset_message' => 'Test Page reset.',
'button_text' => 'Save Changes',
'show_buttons' => true,
'screen_icon' => 'options-general',
'contextual_help' => null,
'sections' => array(
array(
'id' => 'test_section',
'title' => __( 'Test Section', 'motif-core' )
)
),
'settings' => array(
array(
'id' => 'test_section_input',
'label' => 'Test Input',
'desc' => 'Pretty freaking awesome!',
'std' => '',
'type' => 'text',
'section' => 'test_section',
'class' => ''
)
)
)
)
)
)
);
}
}

Categories