Unable to edit a new Custom Taxonomy in Wordpress - wordpress

I have just registered a new Custom Taxonomy in Wordpress (as per the docs). Here is a copy of the code in functions.php for reference:
function people_init() {
// create a new taxonomy
register_taxonomy(
'people',
'post',
array(
'label' => __( 'People' ),
'rewrite' => array( 'slug' => 'person' ),
'capabilities' => array(
'assign_terms' => 'edit_guides',
'edit_terms' => 'publish_guides'
)
)
);
}
add_action( 'init', 'people_init' );
As you can see in the image below, the Taxonomy appears within left-hand navigation but when my (admin) user clicks on the option I am displayed with an You are not allowed to edit this item. error:
Can anyone suggest why this may be?

Almost as soon as I posted this I realised it is the capabilities array. Removing this, so that it reverts to the default allows access as intended.
After further investigation I found that the following was the best settings to get this functioning correctly:
'capabilities' => array(
'manage__terms' => 'edit_posts',
'edit_terms' => 'manage_categories',
'delete_terms' => 'manage_categories',
'assign_terms' => 'edit_posts'
)

Related

How can i automatically create and save a shortcode for each custom post type when user creates a new custom post type

I have a created custom post type using this code,
function adsManager_custom_post_type(){
register_post_type('adsmanager_banner',
array(
'labels' => array(
'name' => __('Banners', 'textdomain'),
'singular_name' => __('Banner', 'textdomain'),
),
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-admin-page',
'rewrite' => array( 'slug' => 'banners' ),
'supports' => array(
'title',
'editor',
'short-code'
)
)
);
}
add_action('init', 'adsManager_custom_post_type');
Now I want to automatically add a shortcode for each custom-post-type (banner) when user creates a new banner and then show the shortcode for each banner in banners screen like,
You have plenty of options if you are looking for an hook after the post is saved. They all comes from the same file which is:
wp-includes/post.php
In that file you can find this action and many more:
do_action( "save_post_{$post->post_type}", $post->ID, $post, true );
After you hook in that you can have this:
add_action('save_post_mycpt','afterMyCptIsSaved');
function afterMyCptIsSaved($postId,$post,$update){
....
your code
....
}

Getting values from Kirki customizer fields in WordPress Theme

I'm using the Kirki plugin to add fields and sections in the WordPress customizer. So far, I can get a field to be added to the customizer, but I'm confused on how to return that data back into my theme. I'm kinda tired so I might be missing something. This is what I have so far:
Kirki::add_config('theme_config_id', array(
'capability' => 'edit_theme_options',
'option_type' => 'theme_mod',
));
Kirki::add_section('footer_section', array(
'title' => __('Footer'),
'description' => __('Add custom footer here'),
'panel' => '', // Not typically needed.
'priority' => 160,
'capability' => 'edit_theme_options',
'theme_supports' => '', // Rarely needed.
));
Kirki::add_field('theme_config_id', [
'type' => 'editor',
'settings' => 'my_setting',
'label' => esc_html__('Footer Content', 'kirki'),
'description' => esc_html__('This content will show in the footer.', 'kirki'),
'section' => 'footer_section',
'default' => '',
]);
I was reading on trying to the values out from here using this:
$value = Kirki::get_option( $config_id, $option_id );
But I'm not sure where (or what) the $config_id or $option_id would be?
I have a feeling that I'm missing something and I've read through the docs and I feel that I'm not getting it.
This is the correct code
$value = Kirki::get_option( 'config_id', 'option_id' );
so in your case,
$value = Kirki::get_option( 'theme_custom', 'footer_content' );
After digging through the internet a little bit, I was able to read through some more documentation as well as some other examples and I was able to figure out what I was doing wrong. Overall, I was close, but I ended up cleaning it up and just using the WordPress get_theme_mod() outright in my template file (in this case it was the footer.php file).
Here's what I ended up with:
Kirki::add_config('theme_custom', array(
'capability' => 'edit_theme_options',
'option_type' => 'theme_mod'
));
Kirki::add_section('footer_section', array(
'title' => __('Footer'),
'description' => __('Add custom footer here'),
'panel' => '', // Not typically needed.
'priority' => 160,
'capability' => 'edit_theme_options',
'theme_supports' => '', // Rarely needed.
));
Kirki::add_field('theme_custom', array(
'type' => 'editor',
'settings' => 'footer_content',
'label' => esc_html__('Footer Content', 'kirki'),
'description' => esc_html__('This content will show in the footer.', 'kirki'),
'section' => 'footer_section',
'default' => '',
'priority' => 10
));
and in my footer.php file, I added this:
<?php $value = get_theme_mod('footer_content', ''); ?>
<?php echo($value); ?>
Granted, this is a super basic way of implementing this. I'm going to try and figure out how to get it to refresh the customizer preview before you publish it. But for now, this seems to be working.

How to extend sorting order of Team Members

I'm using Business Lounge theme by RT themes with Elementor.
Wordpress version is current (5.2.1)
On the team page (Demo: https://businesslounge-demo.rtthemes.com/our-team/) there is a list of cards of team members. I want to change the order of the team members to an option that is not currently selectable.
The team member list is done with a shortcode [staff_box]
In Elementor edit mode I looks like this:
Edit:
The edit form is defined in
wp-content/plugins/businesslounge-extensions/inc/elementor-addons/staff.php
<?php
namespace Elementor;
// ...
class Widget_RT_Staff extends Widget_Base {
// ...
protected function _register_controls() {
// ...
$this->add_control(
'list_orderby',
[
'label' => esc_html_x( 'List Order By', 'Admin Panel','businesslounge' ),
'description' => esc_html_x('Sorts the posts by this parameter', 'Admin Panel','businesslounge' ),
'type' => Controls_Manager::SELECT,
'default' => "date",
"options" => array(
'date' => esc_html_x('Date',"Admin Panel","businesslounge"),
'author' => esc_html_x('Author',"Admin Panel","businesslounge"),
'title' => esc_html_x('Title',"Admin Panel","businesslounge"),
'modified' => esc_html_x('Modified',"Admin Panel","businesslounge"),
'ID' => esc_html_x('ID',"Admin Panel","businesslounge"),
'rand' => esc_html_x('Randomized',"Admin Panel","businesslounge"),
)
]
);
// ...
}
// ...
}
Plugin::instance()->widgets_manager->register_widget_type( new Widget_RT_Staff() );
The edit form is defined in `wp-content/plugins/businesslounge-extensions/inc/editor/staff_box.php`
like so
<?php
vc_map(
array(
'base' => 'staff_box',
'name' => _x( 'Team', 'Admin Panel','businesslounge' ),
'icon' => 'rt_theme rt_team',
'category' => array(_x( 'Content', 'Admin Panel','businesslounge' ), _x( 'Theme Addons', 'Admin Panel','businesslounge' )),
'description' => _x( 'Displays team members', 'Admin Panel','businesslounge' ),
'params' => array(
// ...
array(
'param_name' => 'list_orderby',
'heading' => _x( 'List Order By', 'Admin Panel','businesslounge' ),
"description" => _x("Sorts the posts by this parameter",'Admin Panel','businesslounge'),
'type' => 'dropdown',
"value" => array(
_x('Date','Admin Panel','businesslounge') => 'date',
_x('Author','Admin Panel','businesslounge') => 'author',
_x('Title','Admin Panel','businesslounge') => 'title',
_x('Modified','Admin Panel','businesslounge') => 'modified',
_x('ID','Admin Panel','businesslounge') => 'ID',
_x('Randomized','Admin Panel','businesslounge') => 'rand',
),
'save_always' => true
),
// ...
The output is defined in
wp-content/plugins/businesslounge-extensions/inc/shortcodes/staff_box.php
like so:
<?php
function rt_staff( $atts, $content = null ) {
// ...
//defaults
extract(shortcode_atts(array(
"id" => 'staff-'.rand(100000, 1000000),
"class" => "",
"list_layout" => "1/1",
"list_orderby" => "date",
"list_order" => "DESC",
"ids" => array(),
"box_style" => ""
), $atts));
// ...
//general query
$args=array(
'post_status' => 'publish',
'post_type' => 'staff',
'orderby' => $list_orderby,
'order' => $list_order,
'showposts' => 1000
);
// ...
$theQuery = query_posts($args);
// ...
What I want to do:
Add an option 'post_name' to the select box so that I can sort the team by a different field. I want to add
'Post name' => 'post_name',
How can I do this without changing the original source code?
I already have the child theme of business_lounge theme activated.
Do I need a custom extension for this?
You'd have to modify the original code.
You can't override the function unless the author applied a filter to the value returned, or used an action.
As gael notes in his answer, you can soften the blow of losing changes on updates by copying the original code into your child theme's functions.php then renaming the function - for e.g. to my_rt_staff() before adding in your modifications.
You would however still need to call the my_rt_staff() in the plugin instead of rt_stuff and you would have to make this change whenever the plugin was updated, but you wouldn't lose your code.
(Perhaps you could change the "list_orderby" => "date" in the default shortcode attributes to "list_orderby" => "post_name", as default in your modified my_rt_staff() method so it orders by name as default instead of date)
However, this does not help much in your specific circumstance, as the ideal modification you need to make is to the control itself, on the _register_controls() method in the Widget_RT_Staff class. You can override this by extending Widget_RT_Staff but you would still need to call your new class which results in you modifying the plugin code.
Without seeing how the Widget_RT_Staff class affects the shortocde, I can't be certain this would work, but based on the rt_staff() method, if you use the shortcode as [staff_box orderby="post_name"] you may get your intended result without having to touch any code.
You should be able to modify your plugin without losing update abilities by 'overriding' the function as described here:
https://stackoverflow.com/a/40856197/3623080
Make a copy of the function in your child-theme's functions.php, customize and rename it, use it in place of the original function.
Hope it will help

retrieve customize_register setting from wordpress

I'm trying to retrieve some customizer settings that I initiated.
function notop_customize_register( $wp_customize ) {
$wp_customize->add_section( 'header_images', array(
'title' => 'Header Images',
'priority' => 2,
'description' => "Add images with links to be redirected to when the images are clicked",
) );
// Add color scheme setting and control.
$wp_customize->add_setting( 'header_image_1_setting', array(
'capability' => 'edit_theme_options',
'type' => 'option',
) );
$wp_customize->add_control(new WP_Customize_Upload_Control($wp_customize,'header_image_1_control', array(
'label' => __( 'Import Image', 'notop' ),
'section' => 'header_images',
'settings' => 'header_image_1_setting',
) ) );
}
add_action( 'customize_register', 'notop_customize_register' );
How do I get the url of header_image_1?
I thought it might be get_theme_mod('header_image_1_setting'); but that hasn't worked so far. Also, I'm not sure if I'm actually supposed to call the customize_register file in my index somewhere or if it's okay the way it is.
cale_b's comment is the answer.
get_option('header_image_1_setting')
=> http://...

Cannot access neither $post nor get_post_type() inside template include filter for my custom post type

I'm trying to apply a template_include filter to inflate a template from my plugin.
I've seen a lot of people using the following with success:
function include_template_files($template_file) {
global $post;
$plugindir = dirname( __FILE__ );
if ('mycustomposttype' == get_post_type()){
$templatefilename = 'mytemplate.php';
$template = $plugindir . '/theme_files/' . $templatefilename;
return $template;
}
return $template_file;
}
add_filter( 'template_include', 'include_template_files' );
get_post_type() returns empty, and $post is not instantiated for my custom post. This works fine for WP types (posts, pages, etc.).
What am I doing wrong?
I'm on WP 3.7.1 and I'm using the default twenty thirteen theme.
UPDATE:
I register my type this way:
function register_mycustom_post_type() {
register_post_type( 'mycustomposttype', array(
'labels' => array(
'name' => 'My posts',
'singular_name' => 'My post',
'menu_name' => 'My posts',
'add_new' => 'New custom post',
'add_new_item' => 'Add new custom post',
),
'public' => true,
'show_ui' => true,
'menu_icon' => plugins_url('myicon.png',__FILE__),
'supports' => array( 'title' ,'thumbnail', 'editor' ),
) );
}
add_action('init','register_mycustom_post_type');
SECOND UPDATE (PARTIAL SOLUTION):
The problem is given by permalinks rewrite. If I use the default URLs (index.php?...) it works fine.
SOLUTION
Adding the right rewrite options to my post type solved the problem:
'rewrite' => array(
'slug' => 'mytype',
'with_front' => false
),
Try using the get_queried_object() function like this
$queried = get_queried_object();
print_r($queried);
You should get and object with information from the current page
I answer myself. The problem was permalink rewrite.
Adding the following to register_post_type options solved the problem:
'rewrite' => array(
'slug' => 'mytype',
'with_front' => false
),
Sometimes this can work as well:
get_query_var( 'post_type' );

Resources