Tag: wordpress

the $wpdb object is crazy powerful. with it, you can access almost anything in your database

this is extremely helpful when doing plugin development…

recently at work, i was tasked to do this project:

  • create a wordpress plugin that requires a separate database table
  • the table will house data imported from several excel files
  • admin will be able to add/edit/delete entries
  • create a template that will display the data, dropdowns will help filter information: date, location
  • sort by date, then event name

$wpdb was used heavily in this plugin and it works well

Read more »

originally found this on josh stauffer’s site. just wanna add to my site so i can pull it up easily

$slug = basename(get_permalink());

needed to do this to make wordpress choose the thumbnail image instead of the full-sized image — since some people like to upload gernormous images (recall that wordpress uploads a whole set of images when you upload one)

so this adds a suffix (in this case, “-150×150”) to the of the filename, BUT before the file extension

anyway, here it is:

// set your suffix
$thumbnail_suffix = "-150x150";

// $featured_image is the filename. this would be determined above this code snippet
// determine the length of the filename, including the extension
$length = strlen ($featured_image);

// get the first part of the filename (before the extension)
$temp1 = substr ($featured_image, 0, $length-4);

// get the second part of the filename (the extension)
$temp2 = substr ($featured_image, $length-4, $length);

// stick the suffix in between the two halves
$featured_image_updated = $temp1.$thumbnail_suffix.$temp2;

you might be saying, “but joey.. why don’t you just use something like explode to break up the filename?” well you really don’t know what the filename will be — it could have a bunch of dots in it… if you could figure it out, great. let me know what you did.

at least you know every image file that’ll be uploaded will be a “.” and then three characters after.

was challenged at work and couldn’t think of a proper solution. i knew it was possible, but didn’t know how to execute it

this is what happens when you don’t know all the tools you have available to you…

thank god for the wordpress community

background

this makes absolutely no sense without any background! essentially i have a splash page that goes to several sub-sites. when you get to a sub-site, i want each sub-site’s home to go back to its own home location, not the splash page, which would be

< ?php echo get_option('home'); ?>

code

< ?php //parent variables
	$parent = get_post($post->post_parent);
	$parent_title = get_the_title($parent);
	$grandparent = $parent->post_parent;
	$grandparent_title = get_the_title($grandparent);?>

	< ?php // is the homepage the granparent?
	if ($grandparent == is_page('0')) { ?>
	<li><a href="/<?php echo get_permalink($grandparent); ?>">Back to < ?php echo $grandparent_title; ?></a></li>
	<li><a href="<?php echo get_permalink($post->post_parent); ?>">Back to < ?php echo $parent_title; ?></a></li>

	< ?php // is the homepage the parent?
	} elseif ($post->post_parent ==is_page('0')) {?>
   <li> <a href="<?php echo get_permalink($post->post_parent) ?>">Back to < ?php echo $parent_title; ?></a></li>

    < ?php // I must be a top level page!
	} else { ?> <!-- no parent to show -->
< ?php }?>

here are a bunch of frequently used code snippets i’ve used with many of my projects

* i really should know these by now… but what are ya gonna do?

linking to the homepage

    <a href="<?php echo get_option('home'); ?>">...</a>

    image references

      <img src="<?php bloginfo('stylesheet_directory'); ?>/images/FILE_NAME.jpg"
      alt="<?php bloginfo('name'); ?>" title="<?php bloginfo('name'); ?>" />

      dynamic widgets

        <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('WIDGET_NAME') ) : ?>
        <?php endif; ?>

        then, in functions.php, add this:

        register_sidebar(array('name'=>'WIDGET_NAME',
        'before_widget' => '',
        'after_widget' => '',
        'before_title' => '<h3>',
        'after_title' => '</h3>',
        ));

        custom fields – mentioned before on my site, but just to have ’em all together

          <?php $VARIABLE_NAME=get_post_meta($post->ID, "CUSTOM_FIELD_NAME", true);
          if (get_post_meta($post->ID, "CUSTOM_FIELD_NAME", true)) {
          echo ("<img src=\"FULL_PATH_TO/$VARIABLE_NAME\" />");
          } ?>

          copyright

            &copy; <?php echo date('Y'); ?> - <?php bloginfo('name'); ?>

            on the topic of “figuring things out wrt coding” — i was working on converting a site to wordpress and was trying to figure out how to get a home page movie to appear ONLY on the home page (easily).
            so i looked around and found out there was a wordpress function called “is_front_page()”

            so, along with a nifty fade in/fade out script, i created this in the header.php file:

            <?php
              if ( is_front_page() ) {
              echo "
                <div class=\"hpmovie\">
                  <div id=\"xlide\">
                    <img src=\"wp-content/themes/[THEMENAME]/images/5.jpg\"
                      alt=\"\" />
                    <img src=\"wp-content/themes/[THEMENAME]/images/11.jpg\"
                      alt=\"\" />
                    ...
                  </div>
                </div>
              ";
              }
            ?>

            so basically, if it’s the home page, it’ll write stuff to the page… boom

            as i write the title of this tutorial, i can tell it might get long and/or complicated… i’ll try my best… here we go

            FIRST, you’ll need the tools — download virtual pc (especially if you run windows vista) and then grab an image here — i would suggest getting the IE6 image, since that version of IE is the “worst” of them all
            Read more »

            i feel that i should add a few notes as an addendum to my previous post — call it the “companion pack,” if you will

            • after you modify the “default” folder in the “wp-content\themes” directory, make a copy of it and rename it to something… anything…
              • i found this out the hard way. wordpress just recently did a string of updates (currently 2.8.2) and every time i’d do an update, my site would revert back to the default layout
              • luckily i had all my files locally so i was able to re-upload to the default folder
              • it only took me 3 upgrades to figure out that i had to rename the folder so that wordpress wouldn’t overwrite my work

            Read more »

            hot on the heels of my first tutorial, i come back at ya with a new one.

            i REALLY like using wordpress as a cms — not only for its ease of customization or its extensive library of useful (mostly) plugins, but for these two huge reasons:

            1. ability for the end-user to modify the web site content without breaking the overall site geography
            2. search engine optimization — yea i kinda did mention them earlier, but several plugins can work to help your site get spidered like the best of them.

            so i guess that answers the “why use wordpress?” question — here’s my method of getting a custom theme setup for wordpress
            Read more »