Git Copy

This may be obvious to the git experts, but I am not that.  There also may be a better way to do this, but again, not a git expert.

I have a git repo set up on WPEngine, but they only allow the whole server to be a repo, not just a single plugin.  This was a problem since the whole server directory was not in my current repo.  So it was either make a new repo which was basically a copy of my current repo but a different folder structure or keep on using SFTP.  I decided to make a new duplicate repo.  The problem (which i also had with SFTP) was finding the files that changed and pushing them to the server.  This is both error-prone and slow.  I found a script that could copy all changed files from the last commit into my WPEngine deploy directory and also discovered a way to do this automatically when pushing from my main repo.

Create/Save this script in .git/hooks/pre-push file.  This would be in the main git repo I work in.  This copies all changed files to the WPEngine deployment repo.

#!/bin/bash

TARGET=”/your/deployment/path”
MOST_RECENT=$(git log -n 1 –pretty=format:’%h’)
PREV=$(git log –skip=1 -n 1 –pretty=format:’%h’)

echo “Coping to $TARGET”
for i in $(git diff –name-only $MOST_RECENT $PREV)
do
# First create the target directory, if it doesn’t exist.
mkdir -p “$TARGET/$(dirname $i)”
# Then copy over the file.
cp “$i” “$TARGET/$i”
done
exit 0

Thanks to the following articles about pre-push hooks and how to get the files between commits.

http://www.phprepo.in/2012/04/git-copy-all-changed-files-between-two-commits-with-directory-structure/

http://blog.ittybittyapps.com/blog/2013/09/03/git-pre-push/

Posted in Server Stuff

WordPress the_title filter changed in nav menus

I haven’t had time to look into the exact reason or the exact version this changed, but as of at least WordPress version 4.5.2 the nav menu uses the_title multiple times for one item.

For example if your theme used something like this:


function menu_title_filter( $title ) {
return $title . ' | ';
}
add_filter( 'the_title', 'menu_title_filter' );

It would have one ” | ” after each menu title in previous versions, but in WordPress 4.5 ish it would show two ” | “.  Again, haven’t looked at the exact cause that changes this, but the solution is to check if the item you’re looking at is a nav_menu_item:

function menu_title_filter( $title, $id ) {
$item = get_post( $id );
if ( $item->post_type != 'nav_menu_item') return $title;
return $title . ' | ';
}
add_filter( 'the_title', 'menu_title_filter', 10, 2 );

Posted in Coding Tagged with:

Relative positioning of table rows and row groups is now supported. This site may need to be updated because it may depend on this feature having no effect.

Firefox displayed this warning in the Firebug console.  This warning indicates an absolutely positioned does not consider a table element relatively positioned so it may not appear as expected.  A better explanation is here:

https://www.sencha.com/forum/showthread.php?289315-Relative-positioning-of-table-rows-and-row-groups-is-now-supported.-This-site-may-nee

and solution to fix is here:

http://stackoverflow.com/questions/5148041/does-firefox-support-position-relative-on-table-elements

Posted in Coding Tagged with: , ,

Draggable Icon in Font Awesome

This is weird.  Font Awesome has a ton of fonts, but no draggable icon.  Issue #816 is on github shows much love for this added feature, open since 2013.

A workaround for this suggested by abanctelchevrel:

/* \f142 is ellipsis-v */
/* \202F is thin unbreakable space */
.fa.fa-grip:before {
content: "\f142 \202F \f142 \202F \f142";
}
.fa.fa-grip-large:before {
content: "\f142 \202F \f142 \202F \f142 \202F \f142 \202F \f142 \202F \f142 \202F \f142";
}

fa-grip-large : <i class=”fa fa-grip-large fa-rotate-90″></i>
fa-grip <i class=”fa fa-grip”></i>

Posted in Coding Tagged with:

Link from Google Redirecting

After years of not having a client get hacked, I got 2 in a few weeks.  One client (client A) has a WordPress site which I developed the theme on.  Another client (client B) was brand new, no WordPress.  Client A’s site was redirecting to a porn site whenever a visitor went through a link from Google.  Client B’s site was redirecting to a 404 page (a php file was not found).  Both sites were fine when visiting the direct link.

I found malicious code in Client A’s .htaccess file redirecting anyone visiting from google, bing, or any of the major search engines.  I found and removed the PHP files that were not supposed to be on the site.

Client B’s site was clean as a whistle. Nothing suspicious in the htaccess file and no mysterious php files.  Needed to contact the host about this one.

In searching for more information on how the attackers may have gotten in, I came across this worrisome forum.  Both clients are on GoDaddy so this is probably the issue they both had:

https://wordpress.org/support/topic/malware-redirect-hacks-specific-question-regarding-vulnerabilities?replies=12

Posted in Coding, Server Stuff Tagged with: , , ,

Speed up WP_Query

In WordPress, when a query does not need paging and you only want a limited amount of results, like the last 5 posts, set the ‘no_found_rows’ parameter to true.  This prevents WordPress from doing additional work in counting the total matched rows, thus speeding up your query.

This is already built into get_posts, but not WP_Query.

Example:

$last_5_query = WP_Query( array( ‘no_found_rows’=> true, ‘posts_per_page’=>5 ) );

Posted in Coding Tagged with:

Disable Responsive Images in WordPress

In WordPress 4.4 images are displayed with the srcset attribute.  This attribute allows the browser to select alternative images for best viewing.  Sometimes though this may not be wanted as the image being displayed is the only image you want to show for all users.

To not have any srcset images, use the following:

add_filter( ‘wp_calculate_image_srcset_meta’, ‘__return_null’ );

For other filters new to WordPress 4.4, you can visit the blog post:

Responsive Images in WordPress 4.4

 

One user had a problem with the images in the srcset not having the https protocol and causing security warnings.  This stack overflow post resolves that:

http://wordpress.stackexchange.com/questions/211375/how-do-i-disable-responsive-images-in-wp-4-4

 

Posted in Coding Tagged with:

Escape strings for use in JavaScript

When using parameters from the URL, it should always be sanitized before using it in your javascript.

Using jQuery you can easily do:

var safeString = $(“<span></span>”).text(unsafeString).html();

Using plain javascript:

var entityMap = {
    "&": "&amp;",
    "<": "&lt;",
    ">": "&gt;",
    '"': '&quot;',
    "'": '&#39;',
    "/": '&#x2F;'
  };

  function escapeHtml(string) {
    return String(string).replace(/[&<>"'\/]/g, function (s) {
      return entityMap[s];
    });
  }

The stackoverflow discussion can be found here:  http://stackoverflow.com/questions/24816/escaping-html-strings-with-jquery

 

Posted in Coding Tagged with: , ,

Left Sidebar Under Content

Problem

Having a left sidebar move under the content in a responsive design needs a bit more thinking.  You can’t just do float left width 100% or display block and let the sidebar fall to the next line.

Solution

When the browser width is narrow, use display:table-footer-group for the sidebar, display:table-header-group for the content, and display:table for the parent container.

See more:

http://www.iandevlin.com/blog/2013/06/css/css-stacking-with-display-table

Posted in Coding Tagged with: ,

Run WP-Cron with no visitors

Got a website with noone visits or simply a website that is private but still need WP-Cron to be run?  no problem, you have 2 easy options.

1) Setup a free account with set https://www.setcronjob.com ( i like them better because they enable email notifications on the free account).   Create a cron job to visit wp-cron.php on your domain.

2) set up a scheduled task or cron job on your computer.  Mac OS X and Windows PC.  Problem with this though is your computer needs to be on with internet access, and if you ever buy another computer (and don’t turn your current one on) this job will never run:

https://developer.wordpress.org/plugins/cron/hooking-into-the-system-task-scheduler/

Posted in Coding Tagged with: