Pagination for Custom Post Types

Getting pagination for your custom post type indexes isn’t too hard.

Set the variable $paged:


$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

Be sure that ‘paged’ is in your args for your WP_Query:


'paged' => $paged,

Output the links:


$output .= paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $query_name->max_num_pages
) );

SSL for your local development server

So, some of you may have gotten MAMP to work happily with self-generated SSL certificates. It’s a bit tricky and I’ll assume you’ve got that working.

… a quick tip on getting OS X to shut down the default installed apache so Mamp can run on port 80 and 443:
(found here… https://gist.github.com/jfloff/5138826 )

First of all you need to be able to run MAMP in port 80. This is a “heat check” if you don’t have any process jamming http ports. You can check it like this:

sudo lsof | grep LISTEN

If you do happen to have any process with something like this *:http (LISTEN), you are in trouble. Before with adventure check if it isn’t MAMP itself (yeah, you should close that beforehand)

ps If you don't see MAMP, you are in good hands, I have just the thing for you:
# I've forced the removal of the job
$ launchctl remove org.apache.httpd

# and load it again
$ launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist

# and unload it again
$ launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

Now you should be able to use port 80 (and almost any other) in MAMP. Just go to MAMP > Preferences > Ports Tab and click the Set to default Apache and MySQL ports.

….now back to SSL certs

However, there’s a new wrinkle. Chrome and FF both have decided that self-signed certificates need to be of the Version 3 variety, rather than the plain old ones generated by MAMP. I ran into an issue where chrome was complaining about a missing subjectAltName in the certificate that I had set up.

So, here’s the article I used to get my stuff sort of working:

https://alexanderzeitler.com/articles/Fixing-Chrome-missing_subjectAltName-selfsigned-cert-openssl/

Here’s another version of that:

How to Create Your Own SSL Certificate Authority for Local HTTPS Development

OMG, you say, that’s like waaaaaaa? No worries, I’ll help break it down here and do it a little differently.

They have you create all sorts of scripts. I’m not sure why, probably because it’s the right way to do it, but here’s the straight forward way to set up.

What you are doing is creating your own CA certificate (aka a certificate authority), then using that to create a certificate for your site that needs ssl.

In the following directions, you need to replace YOURLOCALSITEDOMAIN with the domain your are setting up on your MAMP server. You know, like mysite.dev, or sams-site.dev, etc…

Go to the directory, where you store your ssl certificates for MAMP and do the following:

STEP 1
On the command line type out the following:

openssl genrsa -des3 -out rootCA.key 2048
openssl req -x509 -new -nodes -key rootCA.key -sha256 -days 1024 -out rootCA.pem

– this sets your server up to be a CA certificate issuer
– it’s going to ask you a bunch of questions about the country, state, city, and other things. Just answer them with your own info 🙂 The questions will be similar to the parameters you see in the [dn] section in the code below.

STEP 2
Create a file called YOURLOCALSITEDOMAIN.csr.cnf with the following:

[req]
default_bits = 2048
prompt = no
default_md = sha256
distinguished_name = dn

[dn]
C=US
ST=New York
L=Rochester
O=End Point
OU=Testing Domain
emailAddress=your-administrative-address@your-awesome-existing-domain.com
CN = YOURLOCALSITEDOMAIN

– This is a configuration file that will be used when generating your specific site certificates. Change the ST, L, email parameters to whatever you want. I’d go ahead and use your own email.

STEP 3
Then, create a file called v3.ext with the following:

authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = @alt_names

[alt_names]
DNS.1 = localhost
DNS.2 = YOURLOCALSITEDOMAIN

– This is the file that is used by the CA issuer to ensure your cert is version 3 and offers up the named domain as you see in the parameter DNS.2.

STEP 4
Then generate the certificates with this!!! On the command line type out the following (don’t forget to replace the YOURLOCALSITEDOMAIN with whatever development domain you are using:

openssl req -new -sha256 -nodes -out YOURLOCALSITEDOMAIN.csr -newkey rsa:2048 -keyout YOURLOCALSITEDOMAIN.key -config <( cat YOURLOCALSITEDOMAIN.csr.cnf )

openssl x509 -req -in YOURLOCALSITEDOMAIN.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out YOURLOCALSITEDOMAIN.crt -days 5000 -sha256 -extfile v3.ext

Now, when you need to get a second site working, you'll repeat steps 2 through 4. HOWEVER, you won't recreate the v3.ext file. You'll just add a new DNS parameter with your new domain. So, in the above example, I'd be adding DNS.3 = NEWSITEDOMAIN. You'd add a new DNS parameter for each new secure site you do.

STEP 5
Now, open your keychain access app in OS X and add your new certs, then set them to always be trusted. That way your mac will stop throwing warnings. Also, if you are looking at your site in the CodeKit Bonjour URL, then you'll need to add the Temp SSL certificate Codekit creates. You'll find that in the My Certificates section of the Keychain Access app.

I also ran into a thing with iThemes Security. The .htaccess rules were causing redirect loops for the SSL. You could get to the home page, but no secondary pages. Secondary pages resulted in a 500 error. Replacing the iThemes Security SSL feature with the plugin, 'really simple ssl', then clearing out the config that iThemes put in the .htaccess file cleared that right up.

OMG, that made your brain hurt, right? It made mine hurt for a bit too, but hopefully all is working for you now.

The countdown timer and possible redirect

Hey folks, here’s a nifty script that you can use to have a countdown timer. It’s also useful if you want a page to redirect at a specific time of day. For instance, if the sales of a client’s program ends at midnight, you can use this script to countdown to midnight and redirect to a different page after that time.

Got it from here…

https://gist.github.com/nbrombal/63923dda778c67f43ffa

Here’s the code:

<script>
(function($) {

// If target date is specific to one timezone, set to true and specify it
var absoluteTarget = true;
// this number is the number of hours behind Greenwich Mean Time you are setting your clock to.
// In this example, it's set to Pacific time, which is 7 hours behind GMT.
var targetTimeZone = -7;

// You set the target date/time with var targetDateUTC.
//Use this documentation to get an understanding of the numbers below: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/UTC
var targetDateUTC = new Date(Date.UTC(2017,04,24,21,0)).getTime();
var currentDate = new Date();
var timeZoneOffset = (absoluteTarget ? targetTimeZone * -60 : currentDate.getTimezoneOffset()) * 60 * 1000;
var targetDate = targetDateUTC + timeZoneOffset;

var getCountdown = function() {
var totalSeconds = (targetDate - Date.now())/1000;
var days = Math.floor(totalSeconds / (60 * 60 * 24));
var hours = Math.floor(totalSeconds / (60 * 60)) % 24;
var minutes = Math.floor(totalSeconds / 60) % 60;
var seconds = Math.floor(totalSeconds) % 60;

if ( currentDate > targetDate ) {
// Redirect to specific URL
window.location.replace("http://jennjoycoaching.com/");
} else {
// I've commented the below out to NOT show the actual timer.
// if you want to show the timer, see the HTML markup below
// Display the remaining time
//$('.clock .days').html(days);
//$('.clock .hours').html(hours);
//$('.clock .minutes').html(minutes);
//$('.clock .seconds').html(seconds);
}
};

setInterval(getCountdown, 1000);

getCountdown();

})(jQuery);
</script>

HTML Markup for timer


<div class="clock">
<span class="days"></span>
<span class="hours"></span>
<span class="minutes"></span>
<span class="seconds"></span>
</div>

 

Custom Taxonomy Archive Template Paginiation Notes

Five things you need for custom taxonomy archive page pagination working perfectly :

( 1 ) Don’t put exclude_from_search parameter key as register_post_type argument parameter or if mention set it ‘exclude_from_search’ => false. By default it is set false if not mentioned.

( 2 ) The taxonomy that will be use with the custom post type set
‘taxonomies’ => ‘custom_taxonomy_name’ as register_post_type argument parameter or
use register_taxonomy_for_object_type() directly.
Custom taxonomies still need to be registered with register_taxonomy().

( 3 ) While querying within new WP_Query ($args)
i ) If not set in admin static front page use before new WP_Query($args)
$paged = ( get_query_var(‘paged’) ) ? get_query_var(‘paged’) : 1;
and use $query = new WP_Query( array( ‘paged’ => $paged ) );

ii ) If set in admin static front page use before new WP_Query($args)
$paged = ( get_query_var(‘page’) ) ? get_query_var(‘page’) : 1;
and use $query = new WP_Query( array( ‘page’ => $paged ) );
Remember to use ‘posts_per_page’ and ‘paged’ parameter in new WP_Query($arg) argument array.
If not set static front page then you should use ‘page’ parameter in new WP_Query ($arg) argument array

( 4 ) Use WordPress paginate_links( $args ) function like the example below to render pagination in archive template file.

$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%', or '/paged=%#%', // if using pretty permalink
'current' => max( 1, get_query_var('paged') ),
'total' => $query->max_num_pages ) );
// Here $max_num_pages is the properties of new WP_Query() object . It is total number of pages. Is the result of $found_posts / $posts_per_page

( 5 ) The paginate_links() function output the html.

  • .......

If you use bootstrap inject “pagination” class to the

    with the help of javascript or jquery and a nice fancy pagination will be output.

WordPress and their premature attempt to move us forward on MYSQL

Ok, so on April 1, 2015…no kidding…Wordpress announced they are officially supporting the newer charset utf8mb4 in WP version 4.2. This charset is available in mySQL servers version 5.5.3 and higher (I think). This is cool and all, rather hipster, and up to date with best security practices and all that. However, what they completely missed was the fact that a huge portion of the shared server world is running mySQL versions 5.0 and 5.1. If you try to migrate a WordPress site from a server (or local box) that is appropriately running something remotely current to a shared hosting account with the older database, your migration will fail.

Here’s the article where they describe the changes and the beginning of the conversation about how it’s an issue…

https://make.wordpress.org/core/2015/04/02/the-utf8mb4-upgrade/

I’m sure there are thousands of developers wasting time figuring out how to fix this issue. I’ve not found an ‘easy’ fix. Here’s mine clunky version of migrating from development to staging/production…

  1. From phpmyadmin, or your favorite tool, do a full SQL dump.
  2. Run a script or manually change, the URL of the dev site to the new one.
  3. Run a script or manually change ‘utf8mb4_unicode_ci’ to ‘utf8_general_ci’
  4. Run a script or manually change ‘utf8mb4’ to ‘utf8’
  5. Import your new SQL dump into the new database.

This process assumes you’ve migrated the source code of WordPress already.

You can blame ISPs for not getting current, but I think trying to upgrade these older mySQL servers to something more current is an enormous headache for clients and ISPs a like. WordPress should really have paid attention to the reality of this situation and taken measures to mitigate the issue.

Happy hunting for an easier solution, but if you need, the above will work.

Grep find xargs regex and other great tools

This is a little dumping ground for me to use to store useful recipes.

Find a file that was created between two ranges, say on the same day…

touch -t 201608200000 start
touch -t 201608202359 stop
find . -newer start \! -newer stop


Find all files with a particular name or extension and delete if you want

find . -name "*.bak" -type f -delete

Just run without -delete to review before you do it

Find files or directories with a certain permissions set, or without a certain permissions set

find files that don’t have permissions of 644
find /path/to/dir/ -type f ! -perm 0644 -print0

find files that don’t have permissions of 644 and change them
find /path/to/dir/ -type f ! -perm 0644 -print0 | xargs -0 chmod 644

counts all .php and .html files from the current directory that aren’t under the “includes” or “forum” directories.
wc runs wordcount on each file that matches. the “tr” through “bc” takes those numbers and adds them up.

find . -not \( -path ./includes -prune \) -not \( -path ./forum -prune \) -regex ‘.*/.*\(php\|html\)’ -exec wc -l \{\} \; | tr -s ‘ ‘ ‘ ‘ | cut -d ‘ ‘ -f 2 | paste -sd+ – | bc

Partial Content 206 error on fonts after a hosting migration

I’ve recently encountered an issue twice in the last few weeks where the font-face fonts on a website stopped working after a migration to a new hosting provider.

I used the nifty tool, Backup Buddy from iThemes, and all went well, except on the new server the embedded fonts weren’t rendering. :/

After many hours of searching, checking headers, and such, I bothered to look at the console output in Chrome and noticed that there was an error speaking to cross-domain issues specifically regarding the font files.

So what was the issue? In WordPress, you can set the WordPress Address and the Site Address separately. If one is www.somedomain.com and the other is somedomain.com (aka the sub-domain doesn’t match) then you’ll get this pesky error.

Hope that helps.