Category Archives: Code

Get Link to Featured Image for use in View

In functions.php create function and shortcode

 PHP |  copy code |? 
01
/** ***************************************************
02
 * get image src for featured image of post (large version)
03
 */
04
/* Register our short code at the appropriate time during the WordPress Boot process. */
05
add_action( 'init', 'wpk_register_shortcodes');
06
 
07
function wpk_register_shortcodes(){
08
   add_shortcode('my_get_thumbsrc', 'my_get_thumbsrc');
09
}
10
/* Function to return the source url for the featured image of a post */
11
function my_get_thumbsrc($id) {
12
   global $post;
13
    // Here we check if an ID was specified, if not, we use the ID of the current post
14
   $id = ($id) ? $id : $post->ID;
15
   if ( has_post_thumbnail($id)) {
16
//We're getting the url of the large version of the image here, options are 'thumbnail', 'medium', 'large' or 'full'
17
      $image_url = wp_get_attachment_image_src( get_post_thumbnail_id($id), 'large');
18
//  $image_url is an array; It holds url, width and height. See wp_get_attachment_image_src at the WP codex
19
      return $image_url[0]; // return just the url
20
  }
21
}

This shortcode can be used in a View to give link to the image file:

 PHP |  copy code |? 
1
<a href="[my_get_thumbsrc]">[wpv-post-featured-image size="medium"]</a>

Add modal viewer to link:

 PHP |  copy code |? 
1
<a href="[my_get_thumbsrc]" rel="lightbox[post-[wpv-post-id]]">[wpv-post-featured-image size="medium"]</a>

Original post http://wp-types.com/forums/topic/wpv-post-featured-image-as-a-link/

Posted in Code, Plugins, Views, Wordpress 3 | Leave a comment

Show Recent Posts in Template

In template:

 PHP |  copy code |? 
1
 <?php wp_get_recent_posts( $args ) ?> 

Default $args

 PHP |  copy code |? 
01
 <?php $args = array(
02
    'numberposts' => 10,
03
    'offset' => 0,
04
    'category' => 0,
05
    'orderby' => 'post_date',
06
    'order' => 'DESC',
07
    'include' => ,
08
    'exclude' => ,
09
    'meta_key' => ,
10
    'meta_value' =>,
11
    'post_type' => 'post',
12
    'post_status' => 'draft, publish, future, pending, private',
13
    'suppress_filters' => true ); ?> 

Usage:

 PHP |  copy code |? 
01
<h2>Recent Posts</h2>
02
<ul>
03
<?php
04
 $args = array( 'numberposts' => '5' );
05
 $recent_posts = wp_get_recent_posts( $args );
06
 foreach( $recent_posts as $recent ){
07
 echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
08
 }
09
?>
10
</ul>

Posted in Code, Wordpress 3 | Leave a comment

Load Custom JS Scripts

To load a custom js script when, for instance page ID 434 is loaded place the following in theme functions.php:

 PHP |  copy code |? 
1
// load our own validation script on page 434 add_action( 'wp_enqueue_scripts', 'babelscribe_load_scripts' ); function babelscribe_load_scripts() { // register your script location, dependencies and version wp_register_script('validate_script', get_stylesheet_directory_uri().'/js/actions.js', array('jquery')); // enqueue the script if (is_page(434)) { // only load if it is selected page wp_enqueue_script('validate_script'); } } 

Posted in Code, Theming, Tips and Tricks, Wordpress 3 | Leave a comment

Querying the WordPress Database

This is a summary of basic ways to query the WordPress database:

Basic Query returning an object:

 PHP |  copy code |? 
1
<?php
2
   $posts = $wpdb->get_results("SELECT ID, post_title FROM wp_posts WHERE post_status = 'future'
3
   AND post_type='post' ORDER BY post_date ASC LIMIT 0,4")
4
 
5
   // Echo the title of the first scheduled post
6
   echo $posts[0]->post_title;
7
?>

 Basic Query returning a row:

 PHP |  copy code |? 
1
<?php
2
   $posts = $wpdb->get_row("SELECT ID, post_title FROM wp_posts WHERE post_status = 'publish'
3
   AND post_type='post' ORDER BY comment_count DESC LIMIT 0,1")
4
 
5
   // Echo the title of the most commented post
6
   echo $posts->post_title;
7
?>

Basic query returning a variable:

 PHP |  copy code |? 
1
<?php
2
   $email = $wpdb->get_var("SELECT user_email FROM wp_users WHERE user_login = 'danielpataki' ")
3
 
4
   // Echo the user's email address
5
   echo $email;
6
?>

Insert into the database:

Format is as follows:

 PHP |  copy code |? 
1
$wpdb->insert( $table, $data, $format);

for example:

 PHP |  copy code |? 
1
<?php
2
   $wpdb->insert($wpdb->usermeta, array("user_id" => 1, "meta_key" => "awesome_factor", "meta_value" => 10), array("%d", %s", "%d"));
3
 
4
   // Equivalent to:
5
   // INSERT INTO wp_usermeta (user_id, meta_key, meta_value) VALUES (1, "awesome_factor", 10);
6
?>

Formats are:

%s = string

%d = decimal number

%f = floats

Update a row

Format is as follows:

 PHP |  copy code |? 
1
$wpdb->update( $table, $data, $where, $format = null, $where_format = null );

for example:

 PHP |  copy code |? 
1
$wpdb->update( $wpdb->posts, array("post_title" => "Modified Post Title"), array("ID" => 5), array("%s"), array("%d") );

Other Queries:

Use the general query method, for example:

 PHP |  copy code |? 
1
$wpdb->query("DELETE FROM wp_usermeta WHERE meta_key = 'first_login' OR meta_key = 'security_key' ");

SQL Injection Protection

All helper functions above escape the input for you.

The general query doesn’t escape data so use the prepare() function:

 PHP |  copy code |? 
1
$sql = $wpdb->prepare( 'query' [, value_parameter, value_parameter ... ] );

or

 PHP |  copy code |? 
1
$sql = $wpdb->prepare( "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value ) VALUES ( %d, %s, %d )", 3342, 'post_views', 2290 )
2
$wpdb->query($sql);

Insert_ID()

Get last insert auto increment value:

 PHP |  copy code |? 
1
 $meta_id = $wpdb->insert_id;

Table Names

Because table prefix might change use this function to get table name:

 PHP |  copy code |? 
1
//get the name of the posts table (wp_posts)
2
$wpdb->posts
3
 
4
//get name of postmeta table (wp_postsmeta)
5
$wpdb->postsmeta

Add New Table Name

If you create a custom table in the database you need to add it to the $wpdb object by adding this to your themes functions.php file:

 PHP |  copy code |? 
1
// add table to $wpdb class
2
if (!isset($wpdb->paystation_transactions)) {
3
$wpdb->paystation_transactions = $table_prefix . 'paystation_transactions';
4
}
5

Full article is here >>

Posted in Code, Tips and Tricks, Wordpress 3 | Leave a comment

Custom Widget Area

Register a Custom Sidebar as described here

Add widget to the sidebar.

Use the following in your template file to show the sidebar (custom widget area)

 PHP |  copy code |? 
1
// Custom widget Area Start
2
            if( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Left Column Widgets') ) :
3
                endif;
4
// Custom widget Area end

where ‘Left Column Widgets’ is the registered name of the sidebar.

Posted in Code, Theming, Tips and Tricks, Wordpress 3 | Leave a comment

Include Path from Theme Folder

If you can’t use an absolute url to an include file due to server configuration you can use the ABSPATH WordPress constant:

 PHP |  copy code |? 
1
require_once(ABSPATH .'/Connections/boatparts.php');

If ABSPATH is not defined you can use this:

 PHP |  copy code |? 
1
require (dirname(__FILE__)."/makesessionid.php");

Posted in Code, Tips and Tricks, Wordpress 3 | Leave a comment

WP E-Commerce Sales Log Date Wrong Time Zone

Set the server timezone by adding this to top of your  functions.php file of your theme:

 PHP |  copy code |? 
1
<?php
2
    // set default timezone for wp -ecommerce to NZ time
3
    date_default_timezone_set ('Pacific/Auckland' );  
4
?>

Time zones are detailed here:

http://www.php.net/manual/en/timezones.php

Posted in Code, Plugins, WP Ecommerce | 1 Comment

Get Post Type

Get post type for custom post types etc:

 PHP |  copy code |? 
1
get_post_type( $post->ID )

Posted in Code, Tips and Tricks, Wordpress 3 | Leave a comment

Get ID of Parent Page

Test for a parent page and get its ID

 PHP |  copy code |? 
1
 if($post->post_parent){
2
 $parentID = ($post->post_parent);
3
}

Posted in Code, Tips and Tricks, Wordpress 3 | Leave a comment

Custom Field to Define Sidebar

Sidebar can be selected according to a custom field on the page:

 PHP |  copy code |? 
1
<?php
2
global $wp_query;
3
$postid = $wp_query->post->ID;
4
$sidebar = get_post_meta($postid, "sidebar", true);
5
get_sidebar($sidebar);
6
wp_reset_query();
7
?>

Posted in Code, Theming, Tips and Tricks, Wordpress 3 | Leave a comment