All posts by Kenneth Newman

Some of my profiles around the web: Twitter, github, WordPress.org, Google

Multiple LocalHost Sites

I’ve got XAMPP installed on a Windows 7 machine. I wanted a way to test multiple sites locally.

Set up the local host file

In your host file, add (replace with the domains you want):

127.0.0.1    sub.domain.com.dev
127.0.0.1    www.example.com.dev
127.0.0.1    www.unfocus.com.dev

UPDATE: Thanks to xip.io the host file edits are optional if you use the style www.example.com.127.0.0.1.xip.io (replace 127.0.0.1 with your actual IP address). This is compatible with Adobe’s Edge Inspect.

Set up XAMPP config files

In \xampp\apache\conf\httpd.conf uncomment the following at about line 140:

LoadModule vhost_alias_module modules/mod_vhost_alias.so

I’ve used the folder structure of “\xampp\htdocs\www.example.com\web\content\files.php” (for example) and the following config will find the folder with the exact name in the url.

In \xampp\apache\conf\extra\httpd-vhosts.conf:

NameVirtualHost *:80

<virtualhost *:80>
    DocumentRoot C:/xampp/htdocs/
</virtualhost>

# New: No hosts file edits if you use this style
# Replace X.X.X.X with your local IP
<virtualhost X.X.X.X:80>
    # Handle Edge Code IPs
    VirtualDocumentRoot D:/xampp/htdocs/websites/%-7+/web/content/
    ServerAlias *.X.X.X.X.xip.io
    php_admin_value auto_prepend_file D:/xampp/setdocroot.php
</virtualhost>

<virtualhost *:80>
    VirtualDocumentRoot C:/xampp/htdocs/%-2+/web/content/
    ServerAlias *.dev
    php_admin_value auto_prepend_file C:/xampp/setdocroot.php
</virtualhost>

Fix DOCUMENT_ROOT

If you are having issues with DOCUMENT_ROOT, create setdocroot.php at “\xampp\” and put the following in it:

<?php  $_SERVER['DOCUMENT_ROOT'] = str_replace($_SERVER['SCRIPT_NAME'],"",$_SERVER['SCRIPT_FILENAME']);  ?>

References:
https://issues.apache.org/bugzilla/show_bug.cgi?id=26052#c27
http://stackoverflow.com/questions/138162/wildcards-in-a-hosts-file
http://postpostmodern.com/instructional/a-smarter-mamp/

Scripts n Styles Update 3.1

Scripts n Styles received a major update today. The two big features added are LESS.js support and Dynamic Shortcodes! The “Global” Settings page now has a LESS editor with syntax highlighting (via CodeMirror) and on-the-fly compiling so you can see how it’ll be outputted on the theme-side. The per-page meta-box has gained a new tab in which you can create one-off shortcodes which can contain arbitrary HTML content.

Scripts n Styles is a free OpenSource GPL project that you can fork and contribute to on github! (You can also fork and contribute to CodeMirror and LESS.js)

As a Shortcode example: I placed the following html into the Shortcodes tab and gave it the name “tweet test”.

<a href="https://twitter.com/share" data-via="WraithKenny" data-size="large" data-related="unFocusProjects" data-hashtags="ScriptsnStyles">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>

I then use the shortcode [sns_shortcode name="tweet test"] to display:

Scripts n Styles update 2.0.1

Scripts n Styles is a tool to allow admins (and editors in single installs) to add scripts and styles without editing template files, or worrying about authors overwriting the code (code is stripped when an author updates since they don’t have permission to use unfiltered html).

Improvements in version 2.0.1:

The meta box has been improved to provide a tabbed interface for less clutter, and syntax highlight and formating is added using the open-source CodeMirror 2.1.

An option has been added to allow adding script to the head element in addition to the traditional bottom of the page spot.

An Options page (under Tools) has been added so you can add Script n Styles to the entire site, rather then just the individual posts and pages.

Some minor code improvements:

  • Better selection of post_types.
  • micro-optimization for storage of class names.
  • Defined a later priority for Scripts n Styles to print after other scripts and styles.
  • Better adherence to coding standards.
  • began contextual help (notes on capabilities).

WordPress Admin Bar Theme Support

Here’s a quickie for WordPress Theme designers:

If your theme is getting unwanted scroll-bars because of the new Admin Bar is WordPress 3.1, the core team included a way to handle it. Add Theme Support for it!

With a full height layout, you’ll want to avoid adding a margin or padding to a height that is already at 100% because you’ll get useless scrollbars, and no one wants that. Instead, find the first non-full height element (usually #header or some-such), and apply the margin there (either the 28px for the height of the admin bar, or add 28 to the existing margin if the element already has one). In the code snippet below I assumed you’d create an element or assign the class ‘admin-bar-fix’ to an existing element.

In your theme’s function file, add the following and modify as you see fit: (best to leave out the closing php tag though)

<?php
add_action( 'after_setup_theme', 'custom_theme_setup' );
function custom_theme_setup() {
add_theme_support( 'admin-bar', array( 'callback' => 'admin_bar_bump_callback') );
}
function admin_bar_bump_callback() { ?>
<style>.admin-bar-fix { margin-top: 48px; }</style>
<?php } ?>

This snippet is derived from the TwentyTen Theme’s function file. The callback’s original code can be found in the source. Original snippet also found commented in the source (props ocean90).

Basically, by declaring support for the new (as of 3.1) “Admin Bar,” you declare that you can handle how your theme’s content gets “bumped” (by default, it gets pushed down by 28px via a margin on the html.) Most of the time the default behavior is fine… but it’s not fine on theme’s that have a height declaration of 100% (even min-height) or that have external scripts that declare 100% height on the html/body (like Google Translate does).

WordPress’s admin needed a similar treatment but that got patched. The 28px margin is just a default to handle most normal cases. Your theme is your responsibility :-)

Cheers! Hope this saves some time for someone!

Update: Admin Bar Shim!

If you don’t have a 100% height type of layout but are annoyed by improperly scrolling anchored links try the following.

If you add a >div id=”admin-bar-shim”> (for lack of a better name) in your theme surrounding everything inside the body except the wp_footer call (where the admin-bar gets echoed), you can add

.admin-bar #admin-bar-shim {
    position: fixed;
    bottom: 0pt;
    left: 0pt;
    right: 0pt;
    overflow: auto;
    top: 28px;
}

to your style sheet, or use the method above adding the callback. This new method allows anchor links to scroll properly.

Scripts n Styles

Introducing a new plugin for WordPress from unFocus Projects!

Ever need to add a CSS style or some code snippet to just one page or post in WordPress? We release an admin tool to do just that.

On the post edit screen of the admin, Scripts n Styles adds a meta box where you can add JavaScript, CSS or even add class names to the body tag or the post content wrapper (as long as the theme supports wp_head, wp_footer, body_class, and post_class functions and almost all do).

The plugin is available on WordPress.org Extend (and therefor your plugin admin screen :-) ). You can also fork it on Github. It’s licensed GPLv2. (current version 1.0.2)

Enjoy!