• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

My Monkey Do

A Log of Coding Solutions

  • Home
  • Web Hosts
  • Tools
  • About

WP Cron with Basic Authentication

October 12, 2017 by Webhead

Problem

WordPress’ cron doesn’t work when a site has basic authentication.  You basically would get a 401 error.

 

Solution

Thanks to Nick Ohrn (in 2014) this problem is solved with a simple mu-plugin.  You just need to set the username and password in a constant in wp-config and add some code to the mu-plugins directory.  Here is his quick tip.

Filed Under: Coding Tagged With: wordpress

Where is wp_capabilities?

October 5, 2017 by Webhead

Problem

After creating a user with a certain role I need to do a custom sql query where it only selected users of a specific role.  This failed to find anything:

select u.ID, u.display_name, u.user_login
from $wpdb->users as u
inner join $wpdb->usermeta as umr
on ( umr.user_id = u.ID )
and ( umr.meta_key = 'wp_capabilities' )

$user = get_userinfo( $user_id );
var_dump( $user->roles );

get_userinfo got the right role, so saving was correct.

$caps = get_user_meta( $user_id, 'wp_capabilities' );
var_dump( $caps );

$caps was null.  weird.

 

Solution

You learn something new everyday.  After looking through the WordPress core code I found that the meta key is actually composed of the table prefix and ‘capabilities’.  So the correct query would be:

select u.ID, u.display_name, u.user_login
from $wpdb->users as u
inner join $wpdb->usermeta as umr
on ( umr.user_id = u.ID )
and ( umr.meta_key = '" . $wpdb->prefix . "capabilities' )

Filed Under: Coding Tagged With: mysql, php, wordpress

Chrome Zoom Shifts Layout

October 3, 2017 by Webhead

Problem:

When zooming in or out on Chrome, the widths of elements increases very slightly and causes some elements to shift to the next line.

Solution:

The actual problem is Chrome adjusts the widths of elements by tenths.  So something like 141px will turn into 141.1px which in turn causes all your elements to be just slightly bigger and not fit on one line.

I thought the solution to this was to go through all the problem elements and re-set the size to the original size.  However, it turns out you just have to go through the elements in javascript and Chrome will re-set the widths for you.  This leads me to believe this width adjustment on zoom is a bug in Chrome.  Below is what solved my problem in jQuery:


var menuItems = $("#navigation .menu a");
$(window).resize(function() {
menuItems.each(function(i, v){
//by just going through the menu items it seems to fix the width rounding on zoom.
});
});

Filed Under: Coding Tagged With: chrome, jquery

WordPress REST API schema property format: email

April 13, 2017 by Webhead

Problem:  When setting a property type to ’email’ like so:

$schema = array(
'$schema' => 'http://json-schema.org/schema#',
'title' => 'user',
'type' => 'object',
'properties' => array(
'email' => array(
'description' => __( 'The optional email address for the something.' ),
'type' => 'string',
'format' => 'email',
'context' => array( 'edit' ),
'required' => false,
) )

);

WordPress checks if it’s an email like it should, but it does this even if an empty string is passed.  If this needs to be an optional field, the only way I see around it is to not set the format to email.

Filed Under: Coding Tagged With: rest api, wordpress

WordPress Capabilities with map_meta_cap and user_has_cap

February 16, 2017 by Webhead

The two functions, map_meta_cap and user_has_cap allow you to change capabilities on the fly without having to add to roles in the database.  These also allow you to have a ton of flexibility.

Some examples from the talk:

 If you can edit pages, you can edit widgets:
add_filter( 'user_has_cap',
function( $caps ) {
if ( ! empty( $caps['edit_pages'] ) )
$caps['edit_theme_options'] = true;
return $caps;
} );

Give secondary “administrators” less control:
add_filter( 'user_has_cap',
function( $caps, $cap, $args ) {
$user_id = $args[1];
$user = new WP_User( $user_id );
$email = $user->user_email;
if ( $email != get_option( 'admin_email' ) )
$caps['manage_options'] = false;
return $caps;
}, 10, 3 );

Don’t let anyone delete users:
add_filter( 'map_meta_cap',
function( $required_caps, $cap ) {
if ( 'delete_user' == $cap || 'delete_users' == $cap )
$required_caps[] = 'do_not_allow';
return $required_caps;
}, 10, 2 );

Only administrators can delete published posts:
add_filter( 'map_meta_cap',
function( $required_caps, $cap ) {
if ( 'delete_post' == $cap )
$required_caps[] = 'manage_options';
return $required_caps;
}, 10, 2 );

Require editors to approve posts:
add_filter( 'map_meta_cap',
function( $required_caps, $cap ) {
if ( 'publish_post' == $cap || 'publish_posts' == $cap )
$required_caps[] = 'edit_others_post';
return $required_caps;
}, 10, 2 );

Filed Under: Coding

WordPress 4.6 crashed my site

September 2, 2016 by Webhead

Fatal error: Cannot redeclare the_post_thumbnail_caption() (previously declared in ….

This error occurs because the_post_thumbnail_caption is a new function in WordPress 4.6.  However, people have been using this function for over 5 years now.  How?  Stackoverflow and other WordPress forums are flooded with questions asking how they can print out the thumbnail caption.  Since this wasn’t a built-in function before, the answer was to make your own function which was appropriately named the_post_thumbnail_caption.

Many theme authors, myself included, are now guilty of simply copy-pasting this function without adding their own theme prefix.  To avoid errors like this, functions within a theme should be prefixed with something unique to the theme like “mymonkeytheme_the_post_thumbnail_caption”.

If you have this error, contact your theme author and ask them to fix the theme.

 

Filed Under: Coding Tagged With: debug, wordpress

  • « Go to Previous Page
  • Go to page 1
  • Go to page 2
  • Go to page 3
  • Go to page 4
  • Go to page 5
  • Go to page 6
  • Interim pages omitted …
  • Go to page 40
  • Go to Next Page »

Primary Sidebar

Topics

apache apple block editor chrome cms css debug eCommerce embed firebug firefox git gmail goDaddy google hosting htaccess html html 5 IE crap image iPad iPhone javascript jquery linux localization macOS mac os x ms sql mysql open source optimize php php 7.2 rest api seo svg tinymce woocommerce wordpress wpengine xss yii youtube




Categories

  • Coding
  • Off the Shelf
  • Plugins
  • Random Thoughts
  • Server Stuff
  • Tools
  • Uncategorized