Using Global Variables Expensive for PHP

Here is a basic class which you can create once: if ( ! class_exists(‘my_class’) ) : class my_class { public $my_var; // This variable is for a single instance static $instance; function __construct () { // Call other methods like this $this->initialize(); } function initialize () { // Populate your variables here $this->my_var=”any value you … Read more

Modify image while uploading

this is how I’ve done it preg_match(‘/\.[^\.]+$/i’,$file[‘file’][‘name’],$ext); $name = md5(time().$user_id.rand( 5, 97)); $name_one = $name.$ext[0]; $name_blur = $name.’_blur’.$ext[0]; $file[‘file’][‘name’] = $name_one; $upload = wp_upload_dir(); $uploaded_file = wp_handle_upload( $file[‘file’], array( ‘action’=> ‘bp_upload_profile_bg’ ) ); //if file was not uploaded correctly if ( !empty($uploaded_file[‘error’] ) ) { bp_core_add_message( sprintf( __( ‘Upload Failed! Error was: %s’, ‘buddypress’ ), … Read more

How to overwrite youtube embed?

Taking a closer look at the code provided on that site, it appears that the main differences with what it output by default by WordPress is the following: the iframe is wrapped with a div that has a class of embed-container there are CSS styles that are used by that class In WordPress, to wrap … Read more

foreach error on false boolean from get_terms

What you need is pretty much straight out of the Codex: $terms = get_the_terms( $post->ID, ‘on-draught’ ); if ( $terms && ! is_wp_error( $terms ) ) : $draught_links = array(); foreach ( $terms as $term ) { $draught_links[] = $term->name; } get_the_terms() can return a term object, false, or a WP_Error object. You are checking … Read more

Saving Plugin settings to the database

I would store the array to the options able as a serialized value. update_option(‘dw_quotes’, serialize($quotes)); and retrieve with: $quotes = get_option(‘dw_quotes’, null); if ($quotes !== null) { $quotes = unserialize($quotes); } Other things to consider: Add handling in case the quotes option does not exist. Also add handling to delete quotes from the option table … Read more

How to get the post count for the last x days filtering by categories

function get_days_ago_post_count_by_categories($ids){ $date_query = array( array( ‘after’=>’4 days ago’, ‘inclusive’ => true, ) ); $args = array( ‘post_type’ => ‘post’, ‘post_status’=>’publish’, ‘category__in’ => (array)$ids, ‘date_query’ => $date_query, ‘no_found_rows’ => true, ‘suppress_filters’ => true, ‘fields’=>’ids’, ‘posts_per_page’=>-1, ‘orderby’ => ‘ID’ ); $query = new WP_Query( $args ); return $query->post_count; } Note: code isn’t tested and just to … Read more

How to get author ID when an author page is being viewed?

wp_loaded is too early, the request hasn’t been parsed yet – use the action wp instead (called at the end of WP::main(), once the request has been parsed and posts have been queried). To get the user object, just use $author = get_queried_object();

Walker class: Problems with understanding how walk() method can be called without error

Per comments, it’s called with call_user_func_array, so the 1st 2 elements get assigned to the declared arguments $elements and $max_depth, leaving the third element ($r which is the original $args) to be assigned via array_slice to $args. Eg function wpse172812( $elements, $max_depth ) { $args = array_slice(func_get_args(), 2); error_log(“elements=$elements, max_depth=$max_depth, args=” . print_r( $args, true … Read more

Dynamically change feature image in customiser

The customizer has a special control for file uploads. Assuming that you already know how the theme customizer works, you would have to add four controls in this fashion: $wp_customize->add_control( new WP_Customize_Upload_Control( $wp_customize, ‘wpse215632_image_1’, array( ‘label’ => __( ‘First image’, ‘wpse215632_theme’ ), ‘description’ => __( ‘More about first image’, ‘wpse215632_theme’ ), ‘section’ => ‘wpse215632__section_id’, ‘settings’ … Read more

Displaying post per day

This is quite basic but should help you on your way. //Get posts for the current week $args = array( ‘date_query’ => array( array( ‘year’ => date( ‘Y’ ), ‘week’ => date( ‘W’ ), ) ) ); //Check for search query if ( isset( $_GET[‘s’] ) ) { $args[‘s’] = $_GET[‘s’]; } //Create these variables … Read more