Displaying dates using PHP

tomorrow  = mktime(0, 0, 0, date(“m”)  , date(“d”)+1, date(“Y”));
last month = mktime(0, 0, 0, date(“m”)-1, date(“d”),   date(“Y”));
next year  = mktime(0, 0, 0, date(“m”),   date(“d”),   date(“Y”)+1);

Wednesday the 15th =  date(‘l \t\h\e jS’) 
Wednesday = date(“l”)

March 10, 2017 5:16 pm = date(“F j, Y, g:i a”);                
03.10.17 = date(“m.d.y”);                       
10, 3, 2017 = date(“j, n, Y”);                      
 20170310 = date(“Ymd”);                       

 

 

WordPress – CF7 – select fields – CPT

I recently had a challenge.
I had to create a form in WordPress which featured a select box.
Nothing fancy about that, except the owner wanted the select options to be based on the content of a custom post type.
This is proved to be quick trickey because I couldn’t find a CF7 (custom form seven) add on which allowed me to do it, so I had to create a routine to provide the functionality.

 

// this is the custom shortcode I add to the custom form.
wpcf7_add_shortcode( “committee_positions”,”page_handler” );

// this select will support muli-select options    –  so first, lets do the query to get the custome post data
function page_handler(){
$query = new WP_Query(array(
‘post_type’ => ‘committee_positions’,
‘post_status’ => ‘publish’,
‘posts_per_page’ => -1
));
// now lets create a string (called $html) to create the string of to make up field

$html=”<label>What positions are interested in?</label><br/>”;
$html.= “<select name=’comm_positions[]’ multiple=’multiple’ style=’width:58%’>”;
$html.= “<option value=’Undecided’ selected>Undecided</option>”;
while ($query->have_posts()) {
$query->the_post();
$html.= “<option value='”.get_the_title().”‘>”.get_the_title().”</option>”;
}
$html.= “</select>”;
$html.=”<br/><i>You may select more than one</i>”;
// reset the query & return the string  (called by the shortcode)

wp_reset_query();
return $html;
}
// handle the additional data (custom field) that is now available in the standard CF7 form

function filter_wpcf7_posted_data( $posted_data ) {
return $posted_data;
};
$posted_data = apply_filters( ‘wpcf7_posted_data’, $posted_data );
add_filter( ‘wpcf7_posted_data’, ‘filter_wpcf7_posted_data’, 10, 1 );
remove_filter( ‘wpcf7_posted_data’, ‘filter_wpcf7_posted_data’, 10, 1 );

WordPress – adding Google Fonts

There are tons of ways to add and reference custom fonts in WordPress.  Unfortunately not every available option is recommended.  In the worse case scenario you can break your theme.  In equally unsettling circumstances you can find your fonts are no longer available because of a theme update.

So, how should you declare Google fonts so you can safely use them in your WordPress Theme.

The way I’m doing it is to declare and load them in the themes functions.php file.
Depending on whether or not you’re working with protected Child-Themes you should be able to easily locate and update this file.

In the instance below I have referenced two Google fonts, Raleway and Open Sans.
Then I load them when initiating the theme.

function google_fonts() {function google_fonts() {
$query_args = array( ‘family’ => ‘Raleway:400,600,700|Open+Sans’,
‘subset’ => ‘latin,latin-ext’ );

wp_register_style( ‘google_fonts’, add_query_arg( $query_args, “//fonts.googleapis.com/css” ), array(), null );            }            

add_action(‘wp_enqueue_scripts’, ‘google_fonts’)

Having loaded them, they can be used within your css files.
ie.   p{font-family: ‘raleway’; font-weight:600;}

WordPress working with ACF Pro

One of the best features of the WordPress plugin “Advanced Custom Field (ACF)” is the ability not just to add custom fields, but by extension the abilty to create field type called ‘repeat’.
This lets editors add rows of content that is associated with a field  (in a crude way it’s like having relational data).

So, having created the field and poplated it with data (for example, adding multiple images to a post to are presented in a  table)

<?php
// test if the ACF field has rows

if( have_rows(‘image_sliders’) ):
   // run through the rows to get elements in each row

    while ( have_rows(‘image_sliders’) ) : the_row();
      //  get_sub_field is the key

        $theImage=get_sub_field(‘slider_image’);
        $theImg=$theImage[‘url’];
       $theLink=get_sub_field(‘slider_image_url’);
       echo “<div class=’imagecon’><a href=”.$theLink.”><img src=”.$theImg.”></a></div>”;
   endwhile;
   else :
endif;

// clear query after use
wp_reset_query();

?>

Displaying categories in sidebar using javascript, jquery & php

I recently created a theme and then wanted to add a routine to show my Post categories in a vertical hierarchical  menu.
This routine lists the top level category with a count of child posts.
On clicking a category name, the child posts are revealed.

I tried to use a plugin ‘collapse categories’ which should have worked, but the plugin doesn’t work with Bootstrap.css or jquery.1.10+

The routine uses a combination of PHP, Javascript & jQuery.
Work with the routine on the sidebar of the Post of this blog.
The css styles are listed at the bottom of the page

<?php
// set the arguments for the query
$args = array(

‘post_type’ => ‘post’,
‘orderby’ => ‘name’,
‘order’ => ‘ASC’,
‘hide_empty’ => 1,
‘depth’ => 1,
‘posts_per_page’ => -1
);

$categories = get_categories( $args );

// echo each category and create ul (list) for their child posts
echo “<div class=’pcl_category’>”;
foreach ( $categories as $category ) {
echo “<h2 onclick=\”unhide(‘”.$category->slug.”‘);\”>”;       //
echo $category->name .” (“.$category->category_count.”)”;
echo “</h2>”;

$args[‘category’] = $category->term_id;
$posts = get_posts($args); ?>

<div class=”post_cat_list”>
<ul id=”<?php echo “pcl_”.$category->slug;?>”>
<?php foreach($posts as $post) { ?>
<li><a href=”<?php the_permalink()?>”><?php the_title(); ?></a></li>
<?php } ?>
</ul>
</div>
<?php }
echo “</div>”;

// reset query after you’re finished with it
wp_reset_query();

?>

// use standard javascript to work with cookies.
// cookies will determine which category list is open

<script>
writeCookie = function(cname, cvalue, days) {
var dt, expires;
dt = new Date();
dt.setTime(dt.getTime()+(days*24*60*60*1000));
expires = “; expires=”+dt.toGMTString();
document.cookie = cname+”=”+cvalue+expires+’; domain=bluelily.com.au’;
}

function readCookie(name) {
var nameEQ = name + “=”;
var ca = document.cookie.split(‘;’);
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==’ ‘) c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

// use jquery to hide/reveal category lists using jQuery routines

$(function() {
$(‘[id^=”pcl_”]’).hide();
$menuID = readCookie(‘pcl_category’);
$(“#pcl_”+$menuID).show();
});

function unhide($menuID){
$(‘[id^=”pcl_”]’).hide();
$(“#pcl_”+$menuID).delay(50).fadeIn(500);
writeCookie(‘pcl_category’,$menuID,30);
};
</script>

<style>
.pcl_category{text-align: left}
.pcl_category h2{font-size:18px; font-weight: 500; margin-left:20px}
.pcl_category h2:hover{cursor:pointer; text-decoration: underline}
.post_cat_list ul{text-align: left; margin-left:-5px}
.post_cat_list li{line-height: 22px; list-style-type: none}
</style>

WordPress – working with cookies – Javascript

At the clientside, WordPress using Javascript to work in the front end.  Obviously you can use jQuery as well.

Writing Cookies
writeCookie = function(cname, cvalue, days) {
    var dt, expires;
    dt = new Date();
    dt.setTime(dt.getTime()+(days*24*60*60*1000));
    expires = “; expires=”+dt.toGMTString();
   document.cookie = cname+”=”+cvalue+expires+’; domain=bluelily.com.au’;
}

Reading Cookies
function readCookie(name) {
     var nameEQ = name + “=”;
     var ca = document.cookie.split(‘;’);
     for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==’ ‘) c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
       }
return null;
}

Deleting Cookies
setCookie(name, “”, null , null , null, 1);

WordPress – working with cookies – PHP

The WordPress server side script is PHP.
As such you can set, read & delete cookies each time the page is reloaded.

You can use AJAX responses to work with cookies without reloading page, however javascript might be a better approach.

Setting Cookies
setcookie( $name, $value, 30 * DAYS_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );

Reading cookies
if(!isset($_COOKIE[$name])) {
        echo “The cookie: ‘” . $name. “‘ is not set.”;
} else {
        echo “The cookie ‘” . $name. “‘ is set.”;
        echo “Cookie is: ” . $_COOKIE[$name];
}

Deleting and/or cookies
unset( $_COOKIE[$v_username] );
setcookie( $v_username, ”, time() – ( 15 * 60 ) );}