Scripting

Ramblings and links to interesting snippets


Ruby, Haml, and SASS in Dreamweaver CS6

Call me a dinosaur, but I’m still chugging away with Dreamweaver.  I can’t pull myself away from the lovely site management and built in FTP features 🙂

But Dreamweaver lacks support for Ruby, and it’s friends HAML and SASS 🙁  I’ve never gotten HAML and SASS to work 100% with highlighting, but there’s a nice little extension for getting ruby highlighting called “Rubyweaver.”

How can download Rubyweaver from github, https://github.com/cannikin/rubyweaver.  At this time it doesn’t have installation instructions, but I just sent a pull request containing them, so they will probably be there now 🙂  It’s pretty simple, just make certain not to skip the “Run as administrator” step, or the installation won’t take.

Now you can open up ruby files and they’ll have some lovely color coding 🙂  Next, is to make dreamweaver open SASS and HAML files.  There are some tricky steps to get syntax highlighting for SASS, but everything I’ve tried just makes everything pink.  That only really works if you write SASS the same way as you would write CSS.  (I indent my SASS – no curly braces for me!)

In Dreamweaver

  • Go to file>preferences
  • Select File Types/Editors
  • In “Open in code view” add ” .haml .sass”

In notepad

  • Go to C:\Users\~Your Username~\AppData\Roaming\Adobe\Dreamweaver CS6\en_US\Configuration
  • open Extensions.txt
  • on the first line add in ,RB,HAML,SASS
  • change
    HTM,HTML,HTA,HTC,XHTML:HTML Documents
    to
    HTM,HTML,HTA,HTC,XHTML,HAML:HTML Documents
  • and then change
    CSS: Style Sheets
    to
    CSS,SASS:Style Sheets
  • You can also add any other extensions you need to wherever you want.

Now they’ll open up in code view and you’ll be able to search whole folders as well. 🙂

Permalink » No comments

Omit posts with a custom field from WordPress

It took me a while to track this down so I thought I’d post about it.

In a recent project I had the need to omit posts from a WordPress query based on if they had a custom field set to them.

It’s pretty simple once you know how it’s done, imaging you want the ability to remove posts from the homepage by hand.  If you used the custom field name of “hide-me” your query would look like this:

1
query_posts('meta_key=hide-me&meta_compare=NOT EXISTS&meta_value=test');

For “meta_key=hide-me” the hide-me can be whatever custom field you like, and for “meta_value=test” it doesn’t matter at all what you set for “test.”  That’s a little odd, but WordPress needs to have that value, but since you’re only checking weather or not the custom field is there the value doesn’t matter.

The tricky bit it is “meta_compare=NOT EXISTS”  and took a bit of digging to find that.  I had to dig through the documentation of WP_Query to track it down.  It also says that it won’t work for query_posts, but that bit might be out of date, at least for WordPress 3.7.1.  That basically just tell WordPress to check if that custom field doesn’t exist.

It’s pretty easy to use, but I wasn’t able to find a simple write-up on it anywhere and took me a bit of trial and error to get going.  So I thought I’d share 🙂

Permalink » No comments

Gallery of all attached images in WordPress

For a site I’ve been working one I wanted to automatically include a gallery of all the images attached to a post, and I wanted it to display exactly like a standard WordPress gallery would look.  The HTML is modeled after the gallery code from Twenty Thirteen.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
$args = array(
     'numberposts' => -1,
     'orderby' => 'menu_order',
     'order'=> 'ASC',
     'post_mime_type' => 'image',
     'post_parent' => $post->ID,
     'post_status' => null,
     'post_type' => 'attachment'
);
 
$images = get_children( $args );
 
if($images){ ?>
     <h2>Photos</h2>
 
     <div id='gallery-1' class='gallery galleryid-10 gallery-columns-4 gallery-size-thumbnail'>
          <?php foreach($images as $image){
               $image2 = wp_get_attachment_image_src( $image->ID, 'thumbnail'); ?>
               <dl class='gallery-item'><dt class='gallery-icon landscape'>
               <a href='<?php echo $image->guid; ?>'><img width="150" height="150" src="<?php echo $image2[0]; ?>" /></a>
               </dt></dl>
          <?php } ?>
          <br style='clear: both;' />
     </div>
<?php } ?>

Permalink » No comments

Exclude SlideDeck posts in WordPress

Tags: , , , ,

On a WordPress site I’ve been working on I’m using SlideDeck as a feature article slider on the homepage.  The only problem was I have additional articles populating underneath it and the articles from the SlideDeck were showing up down there as well.

We didn’t want that behavior so I researched how to filter out SlideDeck posts from the post loop.  At this time, it’s apparently easy to filter for content that has a custom meta-tag, but you can’t easily filter for content that doesn’t have a custom meta-tag applied to it.  It’s because when a post doesn’t have a custom met-tag attached to it that custom meta-tag doesn’t come up as “null” or “0”  it simply doesn’t come up at all.  So there’s no way to use query_posts to filter out SlideDeck posts, you have to make custom database query and use $wpdb->get_results.

This post from SlideDeck’s customer support website did most of the heavy lifting for me in putting together the exact query I needed to filter out only SlideDeck posts.  The only problem was that this gave me no insight as to how I could paginate the posts.  Thankfully google came to rescue once again and I found this post and this post which helped me piece together the script.  I posted my findings on SlideDeck’s customer service website, but I’m posted it here as well so I can easily find it again if I need it.

So for the code, inside loop.php before have_posts() I put…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if(is_home()) {
    //query_posts(array( 'meta_key' => 'slidedeck_post_featured') );
    global $wpdb;
    $ppp = intval(get_query_var('posts_per_page'));
 
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $offset = ($paged-1) * $ppp;
    $sql = "SELECT p.*
        FROM {$wpdb->posts} AS p
        LEFT JOIN {$wpdb->postmeta} AS pm
        ON pm.post_id = p.ID AND pm.meta_key = %s
        WHERE p.post_status = %s AND p.post_type = %s AND pm.meta_value IS NULL
	ORDER BY p.post_date desc
        LIMIT $offset, $ppp";
    $posts = $wpdb->get_results($wpdb->prepare( $sql, '_slidedeck_post_featured', 'publish', 'post' ));
}

if(is_home())  will make it so it will only filter out SlideDeck posts on the homepage, and apparently the have_posts() works exactly as it normally does even when I change the loop using $wpdb->get_results, which means I don’t have to do much to the rest of the page in order for this to work.  WordPress, you rock <3

This will paginate the posts according to the number of items I have set in the WordPress settings, but for some reason when I did this on the last page it printed out empty posts to fill out that page.

For instance, say I only have 7 posts left on the last page, but I have it set to show a maximum of 10 posts per page – it would print out 3 empty posts on that page.

To combat this I put the following code inside have_posts() so it wouldn’t print anything if the post ID was null.

1
2
3
if(get_the_ID() > 0) {
    //html for posts
}

Pretty straight forward and everything has been working great so far 🙂

 

References

http://support.slidedeck.com/slidedeck/topics/wp_query_a_list_of_posts_not_featured_in_the_slide_deck?from_gsfn=true

http://wordpress.org/support/topic/custom-loop-pagination

http://www.scriptygoddess.com/archives/2011/02/23/wordpress-pagination-woes-solved-i-hope/

Permalink » No comments

Custom Facebook RSS Widget

For a project we wanted to bring in outside links and display them in a box on the homepage.  Instead of building out a whole custom system to handle the link, I suggested posing them through Facebook.

So in order to be able to style the feed however we wanted I put together a simple Curl code in PHP to pull in the Facebook Page RSS feed (every page has a feed it’ll be formated like this => http://www.facebook.com/feeds/page.php?id=245091913103&format=rss20).

This will check if there’s a link inside the body of the status update and use that, but then default to the link to the status message in Facebook if that doesn’t exist.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, "http://www.facebook.com/feeds/page.php?id=245091913103&format=rss20");
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3");
        $xmlTwitter = curl_exec($curl);
        curl_close($curl);
 
        $xmlObjTwitter = simplexml_load_string($xmlTwitter);
        $tempCounter = 0;
 
        foreach ($xmlObjTwitter->channel->item as $item) {                    
            if ( $tempCounter < 9 ){
                preg_match('/(http|https|ftp)(.*?) (id|target)/i', $item->description, $matches);
                $matches[0] = str_replace('" id','',$matches[0]);
                echo '<li><a href="'.($matches[0] ? $matches[0] : $item->link).'" target="_blank">'.$item->title.'</a>
                <div>'.date('F jS, Y',strtotime($item->pubDate)).'</div></li>';
            }
 
            $tempCounter += 1;
        }
 
        echo '</ul>';
?>

Permalink » No comments