WordPress Use Environment Variables for diff domains like local, staging or production — August 15, 2017

WordPress Use Environment Variables for diff domains like local, staging or production

I always use this. Put this in wp-config.php

//GET HOSTNAME INFO
$hostname = $_SERVER['SERVER_NAME']; 

//VERIFY WHICH ENVIRONMENT THE APP IS RUNNING
switch ($hostname) {
    case 'site.dev':
        define('WP_ENV', 'local');
        break;
    case 'staging.site.com':
        define('WP_ENV', 'staging');
        break;
    case 'site.com':
        define('WP_ENV', 'production');
        break;
    default:
        define('WP_ENV', 'local');
}
WordPress filter for all images which appear in a page —
WordPress get posts where date is greater than particular date — July 26, 2017

WordPress get posts where date is greater than particular date

We need to use wordpress date_query for this query. Example:-

$the_query = new WP_Query(array(
	'post_type' => 'estate_property',
	'post_status' => 'publish',
	'posts_per_page' => 10,
	'order' => 'ASC',
	'date_query' => array(
	    array(
	        // 'after'     => 'June 22nd, 2017',
	        // 'before'    => 'December 31st, 2015',
	        'after'    => array(
				'year'  => 2017,
				'month' => 6,
				'day'   => 21,
			),
			'inclusive' => true,
	        
	    ),

	),
));

You can check wordpress documentation for better understanding.

WordPress get current page url with query string — June 7, 2017
wordpress wp_enqueue_script() and ajax example — March 14, 2017

wordpress wp_enqueue_script() and ajax example


//register script

function load_custom_wp_admin_style($hook) {
if($hook != 'settings_page_swot-email-status') {
return;
}
// Register the script
wp_register_script( 'my_custom_adminscript', plugin_dir_url( __FILE__ ) . 'js/admin/adminscript.js', array('jquery') );
// Localize the script with new data
$translation_array = array(
'cresscap_nonce' => wp_create_nonce('swot-form')
);
wp_localize_script( 'my_custom_adminscript', 'adminscript_swotform', $translation_array );
// Enqueued script with localized data.
wp_enqueue_script( 'my_custom_adminscript' );

}
add_action( 'admin_enqueue_scripts', 'load_custom_wp_admin_style' );
/*admin section add scripts*/



/*add menu page in admin area*/
function swot_email_status() 
{

    $args = array(
        'post_type'  => 'swot',
        'posts_per_page' => -1,
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key'     => 'initial_email_status',
                'value'   => array('0', '1'),
                'compare' => 'IN',
            ),
        ),
    );
    $total_waiting_query = new WP_Query( $args );
    $total_waiting =  $total_waiting_query->post_count;
        // echo "Last SQL-Query: {$total_waiting_query->request}";
    wp_reset_query();


    $args = array(
        'post_type'  => 'swot',
        'posts_per_page' => -1,
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key'     => 'initial_email_status',
                'value'   => '2',
                'compare' => '=',
            ),
        ),
    );
    $total_sent_query = new WP_Query( $args );
    $total_sent =  $total_sent_query->post_count;
    wp_reset_query();

    echo '
    ';
}
/*admin section enqueue scripts*/
/*add menu page in admin area*/


/*jquery to make a request*/
jQuery(document).ready(function(){
	jQuery('#send_swot_email_submit').click(function(){
		jQuery(this).prop('disabled', true);
		var send_swot_email_result = jQuery('#send_swot_email_result');
		send_swot_email_result.html('Processing...').show();

		jQuery.ajax({
	        type: 'POST',
	        url: ajaxurl,
	        dataType: 'json',
	        data: {
	            'action':   'send_swot_email_submit',
	            'cresscap_nonce' : adminscript_swotform.cresscap_nonce
	        },
    
	    success: function (response) {
	    	jQuery('#send_swot_email_submit').prop('disabled', false);
	        // console.log(response);
	        send_swot_email_result.html('');
	        if( response.failed_mails )
			{
				var result;
				result = 'Not able to deliver email to : '+ response.failed_mails;
				send_swot_email_result.html(result).show();
			}
			if( response.success_mails )
			{
				var result;
				result = 'Email successfully delivered to : '+ response.success_mails;
				send_swot_email_result.html(result).show();
				// setTimeout(function(){ location.reload(true); }, 2000);

			}
	       
	    }

    });
/*jquery to make a request*/

wordpress ACF create front end form, submit and edit — March 8, 2017

wordpress ACF create front end form, submit and edit

always add acf_form_head(); in head tag
acf_form_head();

$options = array(
'post_id' => $_GET['listing_edit'], // $user_profile,
'field_groups' => array(1199),
'submit_value' => 'Submit',
// ‘uploader’ => ‘basic’,
'class' => 'btn green'

);

acf_form( $options );

$value = get_field( 'add_feed_links_here', $_GET['listing_edit'] );
print_r($value);

And to add validation to this field
add_filter('acf/validate_value/name=add_feed_links_here', 'my_acf_validate_value', 10, 4);

function my_acf_validate_value( $valid, $value, $field, $input ){

// bail early if value is already invalid
if( !$valid ) {

return $valid;

}

// load image data
$data = wp_get_attachment_image_src( $value, 'full' );
$width = $data[1];
$height = $data[2];

if( $width < 960 ) {

$valid = 'Image must be at least 960px wide';

}

// return
return $valid;

}
WordPress not able to upload plugin or theme in linux server — March 7, 2017