settings_fields() doesn’t work for other roles except admin

To change the capability required to save settings you can use the option_page_capability_{$option_page} filter, where {$option_page} is the value passed to settings_field() on your settings page. So if you have this on your settings page: <?php settings_fields( ‘wpse_294802_options’ ); ?> You can change the required capability to save with the option_page_capability_wpse_294802_options filter: function wpse_294802_option_page_capability() { … Read more

Is it safe to call do_action and apply_filters for built-in hooks?

I think there is a difference here between do_action and apply_filters. do_action usually triggers a global state change, either output, enqueuing of resource, initialization of modules, and such. This, like anything which infuance global state can result in surprising side effect when used out of context. apply_filters OTOH behaves much more like a function call, … Read more

Add custom element ID depending where the widget place is, sidebar or footer

Solved it by following the advice from @mmm. Using $args[“id”] did the trick. Using $args[“id”] will return the following: post-sidebar footer-first With a little change, my final code is: public function widget( $args, $instance ) { extract($args); // … <div id=’instafeed-<?php echo explode(“-“, $args[“id”])[0] ;?>’ class=”c-widget-instagram”></div> <?php echo $after_widget; } The result is: <div id=’instafeed-post’ … Read more

Custom plugin – $post_id in wp_head

Use get_queried_object_id() to get the post ID of the current page/post when viewing a single page or post, regardless of whether or not you’re in the loop, or what the global $post variable happens to be at that moment: if ( is_page() || is_single() ) { $post_id = get_queried_object_id(); $meta_alternate = get_post_meta( $post_id, ‘extra_meta_alternate’, true … Read more

js/css updating when making a plugin

This isn’t really a WordPress issue. It’s just how browsers cache styles and scripts so that it doesn’t need to download them again. Renaming the file means the browsers treat them as new files and re-download them. That being said, WordPress does offer a way to get around this issue. But firstly, you should properly … Read more

Access first name and last name from user_register

The Codex is always a good place to look for things. Check out this https://codex.wordpress.org/wp_get_current_user Where you will find this sample code: <?php $current_user = wp_get_current_user(); /** * @example Safe usage: * $current_user = wp_get_current_user(); * if ( ! $current_user->exists() ) { * return; * } */ echo ‘Username: ‘ . $current_user->user_login . ‘<br />’; … Read more