you are here: codestackercodes [RSS] → tag wordpress [RSS]

update wordpress url

use this sql when migrating a website

UPDATE wp_options SET option_value = replace(option_value, 'http://oldsite', 'http://newsite') WHERE option_name = 'home' OR option_name = 'siteurl';

UPDATE wp_posts SET guid = replace(guid, 'http://oldsite','http://newsite');

UPDATE wp_posts SET post_content = replace(post_content, 'http://oldsite', 'http://newsite');

fixing wordpress permissions

find . -name '*.php' | xargs chmod 644
find . -type d | xargs chmod 755

wordpress: posting via php

via http://codex.wordpress.org/Function_Reference/wp_insert_post

<?php

	include "wp-blog-header.php";

	$my_post = array();
	$my_post['post_title'] = 'My post';
	$my_post['post_content'] = 'This is my post.';
	$my_post['post_status'] = 'publish';
	$my_post['post_author'] = 1;
	$my_post['post_category'] = array(13);

	$post_id = wp_insert_post($my_post);

	$post = get_post($post_id);
	wp_redirect($post->guid);

?>

display popular posts (most commented)

<?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 10");
foreach ($result as $topten) {
$postid = $topten->ID;
$title = $topten->post_title;
$commentcount = $topten->comment_count;
if ($commentcount != 0) { ?>
<li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>"><?php echo $title ?></a></li>
<?php } } ?>

display recent updated posts/pages

<?php
	$today = current_time('mysql', 1);
	$howMany = 5; // Number of posts you want to display
	if ( $recentposts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_modified_gmt < '$today' ORDER BY post_modified_gmt DESC LIMIT $howMany")):
?>
<ul>
<?php
foreach ($recentposts as $post) {
	if ($post->post_title == '') $post->post_title = sprintf(__('Post #%s'), $post->ID);
	echo "<li><a href='".get_permalink($post->ID)."'>";
	the_title();
	echo '</a></li>';
}
?>
</ul>
<?php endif; ?>

display rss using wordpress

<?php 
	include_once(ABSPATH.WPINC.'/rss.php');
	wp_rss('http://mysite.com/feed', 5); 
?>

display recent posts

<?php query_posts('showposts=5'); ?>
<ul>
<?php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile;?>
</ul>

count search results

Results for: <?php $allsearch = &new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; echo $key . ' (' . $count . ')'; wp_reset_query(); ?>

reset wordpress admin password via sql

UPDATE wp_users
SET user_pass = MD5('password')
WHERE user_login = 'login';

disable post revisions

in your /wp-config.php

<?php

define('WP_POST_REVISIONS', false);

?>

delete post revisions

DELETE a,b,c  
FROM wp_posts a  
LEFT JOIN wp_term_relationships b ON (a.ID = b.object_id)  
LEFT JOIN wp_postmeta c ON (a.ID = c.post_id)  
WHERE a.post_type = 'revision'

page x of y in wordpress

based on http://design.sparklette.net/teaches/how-to-add-wordpress-pagination-without-a-plugin/

<?php

// paste in functions.php

function pagination($pages = '', $range = 4){
     $showitems = ($range * 2)+1;  

     global $paged;
     if(empty($paged)) $paged = 1;

     if($pages == '') {
         global $wp_query;
         $pages = $wp_query->max_num_pages;
         if(!$pages) {
             $pages = 1;
         }
     }   

     if(1 != $pages) {
         echo "Page ".$paged." of ".$pages;
     }
}

?>



<?php

// in your template 

if (function_exists("pagination")) { pagination($additional_loop->max_num_pages); } 

?>

wordpress $post variables

<?php
$post–>ID # ID of the current post.
$post–>post_category # Retrieves the ID of the post category.
$post–>post_parent # ID of the page parent. Useful for creating custom navigational elements.
$post–>post_title # Post Title
$post–>post_excerpt # Post excerpt
$post–>post_content # Retrieves all of the post content, along with any markup.
$post–>post_name # Retrieving the slug of a post.
$post–>guid # Post Url
$post–>post_author # ID of post author post_parent
$post–>post_type # Returns the type, page or post.
$post–>menu_order # Returns the menu order as set in the post/page editing window. Often menu items are sorted via this value.
$post–>post_date # Retrieves the integer timestamp for when the post was published. The output can be customized. See the php.net date manual.
$post–>post_modified # Retrieves the integer timestamp for when the post was last modified.
$post–>post_status # Retrieves one of five possible posts statuses: publish, private, draft, pending, future.
$post–>comment_count # Returns the number of comments, pings, and trackbacks for a given post.
?>

remove wordpress 3.1 admin bar

paste in functions.php

wp_deregister_script('admin-bar');
wp_deregister_style('admin-bar');
remove_action('wp_footer','wp_admin_bar_render',1000);

clean wordpress gallery css

paste in your functions.php and get these elements:

.gallery {}
.gallery-item {}
.gallery-icon {}
.gallery-caption {}

<?php

function remove_gallery_style() {
  return "<div class='gallery'>";
}
add_filter('gallery_style', 'remove_gallery_style');

?>

wordpress categories dropdown

<!-- old way -->
<?php wp_list_categories('orderby=name&exclude=1&title_li='); ?>

<!-- dropdown -->
<select onchange='document.location.href=this.options[this.selectedIndex].value;'>
	<option><?php echo attribute_escape(__('Select Month')); ?></option>
<?php wp_get_archives('type=monthly&format=option&show_post_count=1'); ?>
</select>

wordpress archives dropdown

<!-- original way -->
<?php wp_get_archives('type=monthly'); ?>

<!-- dropdown -->
<select onchange='document.location.href=this.options[this.selectedIndex].value;'>
	<option><?php echo attribute_escape(__('Select Month')); ?></option>
	<?php wp_get_archives('type=monthly&format=option&show_post_count=1'); ?>
</select>

wordpress post in an external page

<?php
   require('WORDPRES_PATH/wp-load.php');
   global $query_string;
   $qstring_array = array();
   parse_str($query_string,  $qstring_array);
   $args = array_merge($args,$qstring_array);
   query_posts($args) ;
 
 if (have_posts()) : while (have_posts()) : the_post();
         echo "Title :";
         the_title();
        echo "<br/>";
        echo "Author: ";
        the_author();
       echo "<br/><br/>";
      endwhile;
    else:
       echo "No posts";
    endif;
  ?>

wordpress popular posts

<ul>
<?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");
foreach ($result as $post) {
setup_postdata($post);
$postid = $post->ID;
$title = $post->post_title;
$commentcount = $post->comment_count;
if ($commentcount != 0) { ?>
<li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>">
<?php echo $title ?></a> {<?php echo $commentcount ?>}</li>
<?php } } ?>
</ul>

short url in wordpress with tinyurl

paste the function in function.php file and call in you template with:

<?php echo 'ID)).'">short url'; ?>

<?php

function getShortUrl($url) {
  $tinyurl = file_get_contents("http://tinyurl.com/api-create.php?url=".$url);
  return $tinyurl;
}

?>
Displaying Code 1 - 20 of 21 in total