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' => ''
)
)
)
)
)
)
);
}
}
Related
can we add ACF fields to custom admin_menu page?
so I have created an admin page as shown
add_action( 'admin_menu', 'opt_add_admin_menu' );
function opt_add_admin_menu( ) {
add_menu_page( 'opt', 'opt', 'manage_options', 'opt', 'opt_options_page' );
}
so next I need to add an ACF field to this options page
I got this code from ACF but the location still makes a problem for me
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array(
'key' => 'group_6335ef4ab316e',
'title' => 'justfortest',
'fields' => array(
array(
'key' => 'field_6335ef5351c1c',
'label' => 'text',
'name' => 'text',
'type' => 'text',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'default_value' => '',
'placeholder' => '',
'prepend' => '',
'append' => '',
'maxlength' => '',
),
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
),
),
),
'menu_order' => 0,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
));
endif;
'location' => array(
array(
array(
'param' => 'post_type', /* here we need to target the menu options page */
'operator' => '==',
'value' => 'post', /* here to */
),
),
),
I know how to add fields programmatically without ACF but if there is a way to add through ACF it will be so helpful
Create new admin page using acf option page
if( function_exists('acf_add_options_page') ) {
acf_add_options_page(array(
'page_title' => 'opt',
'menu_title' => 'opt',
'menu_slug' => 'opt-general-settings',
'capability' => 'edit_posts',
'redirect' => false
));
}
and select option page ->opt while adding new group
--- You need acf pro version to use this feature
I'm using Metabox Plugin in WP and generate a field using MB online generator.
add_filter( 'rwmb_meta_boxes', 'bq_homemeta_register_meta_boxes' );
function bq_homemeta_register_meta_boxes( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => esc_html__( 'Homepage Meta', 'bequik' ),
'id' => 'home_meta',
'context' => 'normal',
'pages' => 'page',
'fields' => [
[
'type' => 'image_advanced',
'name' => esc_html__( 'Hero Image', 'bequik' ),
'id' => $prefix . 'hero_image',
'max_file_uploads' => 1,
],
],
];
return $meta_boxes;
}
I wanted the metabox field to display only in home page in the WP admin editor.
I tried the include/exclude extension but failed to work.
Thank you!
I would like to ask for help, I am currently learning the WordPress customizer and iplementing it on my project. I have a select field which I would like to increment and decrement on button click (+/-).
$wp_customize->add_section('samplecustom_section' , array(
'title' => esc_html__('Sample Customizer', 'theme_name'),
'priority' => 169,
'capability' => 'edit_theme_options',
));
$wp_customize->add_setting('sample-list', array(
'default' => '',
'transport' => 'refresh',
));
$wp_customize->add_control('sample-list', array(
'label' => esc_html__('Sample Label', 'theme_name'),
'description' => esc_html__('Sample description', 'theme_name'),
'section' => 'samplecustom_section',
'settings' => 'sample-list',
'type' => 'select',
'choices' => array(
'value1' => __( 'Value 1' ),
'value2' => __( 'Value 2' ),
'value3' => __( 'Value 3' ),
));
// button
$wp_customize->add_control('add_button', array(
'section' => 'samplecustom_section',
'settings' => 'sample-list',
'type' => 'button',
'capability' => 'edit_theme_options',
'input_attrs' => array(
'value' => '+',
'class' => 'customizerbutton button-primary',
)
));
here is a sample image where when I click the plus button the select field will increment (or decrement by minus button) creating new ids for the add-control customizer.
How do I get the settings to the frontend? I need to add classes to vc_row.
/**
* Ken Burns Effect for Row.
*/
add_action( 'vc_after_init', 'ken_burns_effect_add_option_to_vc_row' );
function ken_burns_effect_add_option_to_vc_row() {
// Ken Burns Effect Attributes
$ken_burns_effect_attributes =
array(
array(
'type' => 'checkbox',
'heading' => __( 'Ken Burns for image background', 'ken_burns_effect' ),
'param_name' => 'enable_ken_burns_effect',
'value' => array(
__( 'Yes', 'ken_burns_effect' ) => 'yes',
),
'description' => 'Check this box if you want to enable ken burns effect for this row.',
),
array(
'type' => 'dropdown',
'heading' => __( 'Direction', 'ken_burns_effect' ),
'param_name' => 'direction_ken_burns_effect',
'value' => array(
'Zoom In' => 'zoom_in',
'Zoom Out' => 'zoom_out',
),
'description' => __( '', 'ken_burns_effect' ),
'dependency' => array(
'element' => 'enable_ken_burns_effect',
'value' => array( 'yes' ),
),
),
array(
'type' => 'textfield',
'heading' => __( 'Transition speed', 'ken_burns_effect' ),
'param_name' => 'transition_speed_ken_burns_effect',
'value' => '',
'description' => __( '', 'ken_burns_effect' ),
'dependency' => array(
'element' => 'enable_ken_burns_effect',
'value' => array( 'yes' ),
),
),
);
vc_add_params( 'vc_row', $ken_burns_effect_attributes);
}
You will have to override the vc_row.php by copying it to your theme or plugin.
You can check their documentation on how to set the override directory: https://kb.wpbakery.com/docs/inner-api/vc_set_shortcodes_templates_dir/
Then you can use your custom params inside vc_row.php using their param_name eg:
$enable_ken_burns_effect;
$direction_ken_burns_effect;
$transition_speed_ken_burns_effect;
Keep in mind that you have to override vc_row_inner.php too if you want to use options there. Same goes for section and columns.
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(),
)