Wordpress - Adding to Redux Theme Options using Child Theme - php

I am using a wordpress theme that has it's own framwork which is based upon redux framework I think. I am modifying this theme using a child theme. I want to add to the Theme Options in the backend and I found a function within the files of the parent theme that seems to be exactly what I need:
/*
*
* Custom function for filtering the sections array. Good for child themes to override or add to the sections.
* Simply include this function in the child themes functions.php file.
*
* NOTE: the defined constansts for URLs, and directories will NOT be available at this point in a child theme,
* so you must use get_template_directory_uri() if you want to use any of the built in icons
*
*/
function add_another_section($sections){
//$sections = array();
$sections[] = array(
'title' => __('A Section added by hook', 'swift-framework-admin'),
'desc' => __('<p class="description">This is a section created by adding a filter to the sections array. Can be used by child themes to add/remove sections from the options.</p>', 'swift-framework-admin'),
// Redux ships with the glyphicons free icon pack, included in the options folder.
// Feel free to use them, add your own icons, or leave this blank for the default.
'icon' => trailingslashit(get_template_directory_uri()) . 'options/img/icons/glyphicons_062_attach.png',
// Leave this as a blank section, no options just some intro text set above.
'fields' => array()
);
return $sections;
}
//add_filter('redux-opts-sections-twenty_eleven', 'add_another_section');
I have added this function to my child theme's functions.php and uncommented the add_filter. However, this does not seem to work and no new section has been added.
I came across this discussion elsewhere which suggests the name of the function needs to be changed (I was getting the same error mentioned on there). I have done that and it still won't work.
Here is what I have in my child theme functions.php
function add_another_section_bl($sections){
$sections = array();
$sections[] = array(
'title' => __('A Section added by hook', 'swift-framework-admin'),
'desc' => __('<p class="description">This is a section created by adding a filter to the sections array. Can be used by child themes to add/remove sections from the options.</p>', 'swift-framework-admin'),
// Redux ships with the glyphicons free icon pack, included in the options folder.
// Feel free to use them, add your own icons, or leave this blank for the default.
'icon' => trailingslashit(get_template_directory_uri()) . 'options/img/icons/glyphicons_062_attach.png',
// Leave this as a blank section, no options just some intro text set above.
'fields' => array()
);
return $sections;
}
add_filter('redux-opts-sections-twenty_eleven', 'add_another_section_bl');
I wasn't sure if the filter name 'redux-opts-sections-twenty_eleven' needs editing as it mentions the twenty eleven theme. I have tried it with different theme names at the end instead of twenty_eleven and that had no effect.
Any help would be greatly appreciated! On a side note I have been able to accomplish adding new options to Theme Options by copying over the whole framwork folder into my child theme and defining the path to the framework in the child theme's functions.php. I just felt that there should be a much slicker, neater way to achieve this and I thought that function sounded perfect.
Many thanks.

Lead dev of Redux Framework here. This solution only works if you're using Redux Framework 3.1+. If you have an older version, install the Redux Framework plugin and it will override the version inside your theme.
First go to the current option panel. Open up a javascript console (use chrome or firefox) and type: redux.args.opt_name. That will echo out a name. Copy that and paste it into this function replacing OPT_NAME with the name that was echo'd out:
function add_another_section_bl($sections){
$sections = array(); // Delete this if you want to keep original sections!
$sections[] = array(
'title' => __('A Section added by hook', 'swift-framework-admin'),
'desc' => __('<p class="description">This is a section created by adding a filter to the sections array. Can be used by child themes to add/remove sections from the options.</p>', 'swift-framework-admin'),
// Redux ships with the glyphicons free icon pack, included in the options folder.
// Feel free to use them, add your own icons, or leave this blank for the default.
'icon' => trailingslashit(get_template_directory_uri()) . 'options/img/icons/glyphicons_062_attach.png',
// Leave this as a blank section, no options just some intro text set above.
'fields' => array()
);
return $sections;
}
// In this example OPT_NAME is the returned opt_name.
add_filter("redux/options/OPT_NAME/sections", 'add_another_section_bl');
Good luck!
** UPDATE **
Also with the Redux API you can easily add a new section that way.
Redux::addSection(array(
'title' => __('A Section added by hook', 'swift-framework-admin'),
'desc' => __('<p class="description">This is a section created by adding a filter to the sections array. Can be used by child themes to add/remove sections from the options.</p>', 'swift-framework-admin'),
// Redux ships with the glyphicons free icon pack, included in the options folder.
// Feel free to use them, add your own icons, or leave this blank for the default.
'icon' => trailingslashit(get_template_directory_uri()) . 'options/img/icons/glyphicons_062_attach.png',
// Leave this as a blank section, no options just some intro text set above.
'fields' => array()
))
That makes it a wee bit easier using our API I believe we released in Redux 3.2...

If you are using redux options with plugin your filter won't work inside a theme
for that make sure you put code inside a plugin.
Here is code that will work with theme and plugin as well!
function add_social_media_options($sections) {
$sections[] = array(
// Redux ships with the glyphicons free icon pack, included in the options folder.
// Feel free to use them, add your own icons, or leave this blank for the default.
'icon' => 'el-icon-wrench',
'title' => esc_html__('Social Media Settings', 'textdomain'),
'desc' => esc_html__('These are settings social media link', 'textdomain'),
'fields' => array(
array(
'id' => 'facebook_link',
'type' => 'text',
'url' => true,
'title' => __('Facebook link', 'textdomain'),
'compiler' => 'true',
//'mode' => false, // Can be set to false to allow any media type, or can also be set to any mime type.
'desc' => __('', 'textdomain'),
'default' => ''
),
array(
'id' => 'twitter_link',
'type' => 'text',
'url' => true,
'title' => __('Twitter link', 'textdomain'),
'compiler' => 'true',
//'mode' => false, // Can be set to false to allow any media type, or can also be set to any mime type.
'desc' => __('', 'textdomain'),
'default' => ''
),
)
);
return $sections;
}
add_filter("redux/options/redux_demo/sections", 'add_social_media_options');

Related

CPT remove single view Wordpress frontend

I added a custom post type 'Catalog'.
This CPT makes it posible to create A list of items like a catalog.
For posts Wordpress has always a single view page on the frontend and I want remove these and show an 404 error if somebody tried visit the url.
register_post_type( 'catalog',
array(
'labels' => array(
'name' => 'Catalogus',
'add_new' => 'Nieuw item',
'add_new_item' => 'Nieuw item toevoegen',
'new_item' => 'Nieuw item',
),
'public' => false,
'show_in_rest' => false,
'menu_icon' => 'dashicons-store',
'menu_position' => 2
)
);
I set the public on false but this removes the whole CPT on the front and adminside.
Who can help me?
When you register a custom post type, a number of other values will set their defaults based on the value used for public. By setting public to false, the argument to show the UI in the admin panel will also have defaulted to false.
Pass in show_ui to control whether it's displayed in the admin panel:
register_post_type( 'catalog', [
'labels' => [
// labels...
]
'public' => false,
'show_in_rest' => false, // default is false so probably not needed
'show_ui' => true, // show the admin UI for the CPT even when public is false
'menu_icon' => 'dashicons-store',
'menu_position' => 2
] );
https://developer.wordpress.org/reference/functions/register_post_type/#public
For anyone in the community that's using the Custom Post Type's UI (CPT UI) plugin, just set "Publicly Queryable" to false. This will disable the single page for your CPT.
Any (custom) post types is ruled by the Wordpress Template Hierarchy.
As you can see here with the Visual Overview, any post type will be displayed by the following templates:
Custom Post Type
$custom.php → (fallback) single-$posttype-$slug.php → (fallback) single-$posttype.php → (fallback) single.php → (fallback) singular.php
Blog Post
$custom.php → (fallback) single.php → (fallback) singular.php
One way to remove the single custom post type display ability and keeping the query ability is to remove and add the adequate templates.
Following best practices, by removing single.php & singular.php and not including any custom templates, you will effectively remove a custom post type ability to produce a single template which will redirect the request to a 404.php and corresponding fallbacks.
To make sure our blog posts still renders and have a single post type display ability, we need to include a single-post.php which will only display theme's blog posts.
Here is a minimal graphical representation of a theme's folder with no single custom post type ability and a single blog post post ability.
myTheme/
├── style.css/
├── single-post.php/
└── index.php/

How to override woocommerce customize panel file (in folder includes)

I'm overriding Woocommerce theme. And now I have to override customize panel. There is a few woocommerce default sections in Customize panel to change some Woocommerce data, labels etc. (such as image size, number of post per page, privacy text...). And I want to add my own customize panel fields to make some data in my child theme to be easy to change (they should be placed in the same control panel sections next to Woocommerce default fields).
There is Woocommerce/Includes/Customizer/*customizer files*. So I can simply override that files in Woocommerce folder but when Woocommerce will be updated I'll lose my changes.
So I need to add my customize fields (and I think the best way is write them somewhere in my child theme maybe in functions.php but I didn't manage to do it yet) in the same Woocommerce control panel tabs and sections. (For example in section Product Catalog (that's woocommerce default section) I want to make changeable my own filter label).
Is there any way to do it?
Thanks in advance))
To add your own section with settings under WooCommerce customize panel, just add follows codes snippets in your active theme's functions.php -
add_action( 'customize_register', 'my_custom_customize_register', 99 );
function my_custom_customize_register( $wp_customize ) {
$wp_customize->add_section(
'my_wc_custom_section',
array(
'title' => __( 'My Custom Section', 'text-domain' ),
'priority' => 20,
'panel' => 'woocommerce',
'description' => '',
)
);
$wp_customize->add_setting( 'my_wc_custom_section_settings', array( 'transport' => 'postMessage' ) );
$wp_customize->add_control( 'my_wc_custom_section_settings_control',
array(
'label' => __( 'Custom Text', 'text-domain' ),
'type' => 'text',
'settings' => 'my_wc_custom_section_settings',
'section' => 'my_wc_custom_section',
'priority' => 20,
)
);
}
To display your saved field's value from your custom section settings, just use follows -
echo get_theme_mod( 'my_wc_custom_section_settings' );
Thats it.

Wordpress customizer: Add settings and controls dynamically

In order to simplify things, I have a social icon in my WP theme, that I want to enable users to modify via the WP customizer:
$wp_customize->add_setting(
'tcx_social_twitter',
array(
'default' => 'fa fa-twitter'
)
);
$wp_customize->add_control(
'tcx_social_twitter',
array(
'section' => 'tcx_social',
'label' => 'Social links',
'type' => 'text'
)
);
Then <?php echo get_theme_mod( 'tcx_social_twitter' ); ?> in my theme layout to show the icon to the end user. Everything works great so far.
Now I want to enable future adinistrators of my theme to add as many social icons, as they want. I believe this requires adding settings and controls dynamically, however I faild to find any information on this question in the official WP docs. Any help would be greatly appreciated!

SilverStripe wysiwyg style constraints

Is there any way to prevent certain custom wysiwyg styles to be applied to certain HTML elements?
Suppose I have two styles called "red" and "framed". One colours text red and the latter puts a frame around photos.
Can I control which styles are available when highlighting text or an image? For example, I want the style "framed" not to be available when highlighting text and the style "red" not showing in the styles dropdown menu when highlighting an image.
Tinymce configuration is kinda tricky in silverstripe, though it looks easy once you got it running.
Jonom wrote down some configs and put it on Github, called tinytidy module, there you might find a possile solution. So best is to try out this module as it adds some custom css to the editor.
Copied from there, put in your /mysite/_config.php:
$formats = array(
// Define the styles that will be available in TinyMCE's dropdown style menu
// * Use 'selector' to specify which elements a style can be applied to
// * See Headings example below for explanation of different settings
// * Using 'classes' allows a class to be combined with others while 'attributes'=>'style' removes other classes before applying
// Text styles
array(
'title' => 'Selected text'
),
array(
'title' => 'highlight red',
'classes' => 'red',
'inline' => 'span',
'selector' => 'i,em,b,strong,a'
),
array(
'title' => 'Images',
),
array(
'title' => 'Put a frame around a photo',
'attributes' => array('class'=>'framed'),
'selector' => 'img'
)
);
//Set the dropdown menu options
HtmlEditorConfig::get('cms')->setOption('style_formats',$formats);
hope that helps, wmk

Create new admin section in wordpress - for creating office-pages

Ok, I have been looking for hours and I have to say I am really lost. I am trying to create a new section in the admin section of wordpress that should enable the user to create a new custom "office" page.
There is really many office pages on the site I have been working on (over 30), each with its opening hours, map, and images. I assume the client will want to add more later (or remove them) and they would like to manage it through Wordpress. That would mean adding a section that would enable them to put in the name of the office, opening hours, images and the location and it would create a new office page. I am rather a front-end developer and I have never worked with Wordpress before. I understand the loop, etc, I have read several things about Themes and how to create them but I am seriously stuck with how to create a section in admin area that would enable page creation/deletion with certain options.
Any help is greatly appreciated, just please point me to the correct direction. Web pages, WP codex, tutorials, youtube... whatever that helps. Thanks a bunch!
You can make custom post type..
By writing Code in function.php
function function-name(){
register_post_type( 'post name',
array(
'labels' => array(
'name' => __( 'post name' ),
'singular_name' => __( 'post name' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'post name'),
'supports' => array('title','editor','author','thumbnail','comments','custom-fields'),
)
);
}
add_action('init', 'function-name');
3 steps : -
1. create office post type by using Register Post Type
2. Create office categories by using Register taxonomy
3. create a meta box by using Add Meta Box for extra fields which wordpress doesnt offer by default like (office hours )
hope it helps !

Categories