Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
9 total  XML / RSS feed 

quick fix for the broken wp_newCategory function in the wordpress xmlrpc.php

// just replace the original one and call each arg as a var from the xml client
// if you dont need desc and parent_id (pid) you also dont need to send the slug
// because its slugged by wp_insert_category anyway... ;)
// it also returns the category id if it already exists without adding a new one

function wp_newCategory($args) {
global $wpdb;
$this->escape($args);

$blog_id = (int) $args[0];
$username = $args[1];
$password = $args[2];
$category_name = $args[3];
$category_slug = $args[4];
$category_pid = $args[5];
$category_desc = $args[6];

if(!$this->login_pass_ok($username, $password)) {
return($this->error);
}

// Set the user context and make sure they are
// allowed to add a category.
set_current_user(0, $username);
if(!current_user_can("manage_categories", $page_id)) {
return(new IXR_Error(401, __("Sorry, you do not have the right to add a category.")));
}

// We need this to make use of the wp_insert_category()
// funciton.
require_once(ABSPATH . "wp-admin/admin-db.php");

$cats = $wpdb->get_results("SELECT cat_ID,cat_name,category_parent FROM $wpdb->categories WHERE cat_name = '".mysql_escape_string( $category_name )."' LIMIT 1", ARRAY_A);
if( count( $cats ) == 1 ) return( $cats[0]['cat_ID'] );

// If no slug was provided make it empty so that
// WordPress will generate one.
if(empty($category_slug)) {
$category_slug = "";
}

// If no parent_id was provided make it empty
// so that it will be a top level page (no parent).
if ( !isset($category_pid) )
$category_pid = "";

// If no description was provided make it empty.
if(empty($category_desc)) {
$category_desc = "";
}

$new_category = array(
"cat_name" => $category_name,
"category_nicename" => $category_slug,
"category_parent" => $category_pid,
"category_description" => $category_desc
);

$cat_id = wp_insert_category($new_category);
if(!$cat_id) {
return(new IXR_Error(500, __("Sorry, the new category failed.")));
}

return($cat_id);
}

How to update something using a patch (Wordpress, pmwiki etc.)

After unzipping, taring or whatever archive you used copy thepatch over the old files, replacing them.

Copy the files in your newly extracted directory (pmwik-new/wordpress-patch) over the files of your existing software installation. For example, if your existing PmWiki/Wordpress installation is in a directory called pmwiki/wordpress, then one way to copy the new files over the existing ones is to enter the command:
cp -a pmwiki-new/. pmwiki


Note that BSD systems will not have the -a option as a command-line argument for cp, but that's okay, since it's just shorthand for cp -dpR, so use that instead of -a.

On (some) FreeBSD servers and Mac OS X systems you need to use
cp -Rpv pmwiki-new/. pmwiki 

This works well on TxD shared hosting.

Scrobbler

 <h2>Music at <a href="http://www.last.fm/user/kyokokarl" target="_blank">lastFM</a></h2>
<BR>
<embed 
    src="http://static.last.fm/quilts/3/quilts_main.swf" 
    flashvars="type=user&variable=kyokokarl&file=topalbums&bgColor=black&configMode=true" 
    quality="high" 
    bgcolor="#ffffff" 
    width="250" 
    height="90" 
    name="quilts_main" 
    align="middle" 
    allowScriptAccess="never" 
    type="application/x-shockwave-flash" 
    pluginspage="http://www.macromedia.com/go/getflashplayer" />

<?php if (function_exists('get_scrobbler')) {get_scrobbler(); } ?>
<?php if (function_exists('recent_tracks')) {recent_tracks('kyokokarl'); } ?>

</div>

Customizing flickrRSS

// description of your code here
Customizing flickrRSS

.sb-flickr div img {
border: 0px;
padding: 5px;
height: 50px;
width: 50px;
}
 .sb-flickr div a img {
border: 1px solid #ccc;
padding: 3px;
margin: 5px 3px 0;
}
.sb-flickr div a:hover img {
border: 1px solid #999;
}


<div class="sb-flickr">
<div>
<h2>FlickR</h2>
<?php get_flickrRSS(); ?>
</div>
</div>

Export WordPress tables

// description
Export only WordPress tables from mysql database.

mysqldump -u dbname -p username --skip-opt -B --tables wp_categories wp_comments wp_link2cat wp_links wp_options wp_post2cat wp_postmeta wp_posts wp_usermeta wp_users > wordpress.sql


WordPress custom post listing

// This pulls out posts from WordPress and displays them on a Page Template, along with some Custom Fields.

<?php
/*
Template Name: Offerings
*/

get_header(); ?>

        <div id="content" class="widecolumn">
                <div id="copy">
            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                        <h1 id="post-<?php the_ID(); ?>"><?php the_title(); ?></h1>
                                <?php the_content('<p class="serif">Read the rest of this page &raquo;</p>'); ?>
                                <?php link_pages('<p><strong>Pages:</strong> ', '</p>', 'number'); ?>
                
                        <? $posts = get_posts('numberposts=50&category=1'); 
                           if($posts) : ?>
                                
                                        <table border="0" cellpadding="5" cellspacing="0" width="100%">
                                        <tr style="background:#cde; font-size:12pt; font-weight:bold">
                                            <td width="40%">Description</td>
                                            <td width="20%">Location</td>
                                            <td width="20%">Revenue</td>
                                            <td width="20%">Earnings</td>
                                        </tr>
                                
                                        <? $i=0;
                                        foreach($posts as $post) : setup_postdata($post); 
                                                $rev = get_post_custom_values('revenue');
                                                $rev1 = trim($rev[0], "$");
                                                $rev2 = str_replace(",", "", $rev1);
                                                $revs[] = preg_replace('/ (\w* ?)?\d*/i', '', $rev2); /* strips space+words+space+digits */
                                                $post_ids[] = $post->ID;
                                        endforeach;
                                
                                        $together = array_combine($post_ids, $revs);
                                        arsort($together);
                                        
                                        if($together) : foreach($together as $key => $value) : get_a_post($key); ?>                       
                                        
                                                <tr<? if($i % 2) { echo ' style="background:#def"'; } ?>>
                                                        <td><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></td>
                                                        <td><?php $loc = get_post_custom_values('location'); echo $loc[0]; ?></td>
                                                        <td><?php $rev = get_post_custom_values('revenue'); if($rev) { echo $rev[0]; } else { echo '&nbsp;'; } ?></td>
                                                        <td><?php $adj = get_post_custom_values('adj_earn'); if($adj) { echo $adj[0]; } else { echo '&nbsp;'; } ?></td>
                                                </tr>
                                        <? $i++; endforeach; endif; ?>
                                
                                </table><br />
                                
                        <? endif; ?>
                        
                <?php endwhile; endif; ?>   
                </div>

<?php get_footer(); ?>

Display In 2-Column Table

The below can be applied to MT, EE, etc, and will display the content in a 2 column table. The EE tags below are just an example, it will work equally well if you replace it with MT, Wordpress, etc, tags.

<? $set_table="0"; ?>

<table cellpadding="5" border="0">
{exp:gallery:categories gallery="{gallery_name}"}
<? 
$fs_table = $set_table +1;
if ($set_table == "1") {echo"<tr>";} ?>


Insert other tags here.


<? if ($set_table == "2") {echo"</tr>";  $set_table="0";} ?>
{/exp:gallery:categories}
</table>

Ruby script to post daily del.icio.us links to a blog.

This is a ruby script I (sloppily) cooked up to post daily del.icio.us links to my blog. It requires the Rubilicious library, and (because I'm lazy) is hardcoded to point at my blog.

Change the line

server = XMLRPC::Client.new( "cracker.mocephus.org", "/xmlrpc.php")


So that it points to your blog and your xmlrpc endpoint. Only tested with my Wordpress blog.

An example run would be:

./daily_delicious.rb myblogusername myblogpassword mydelicioususername mydeliciouspassword 1


The last argument is how many days back you'd like to gather links for.

Example post here

#!/usr/local/bin/ruby

require "xmlrpc/client"
require 'rubygems'
require_gem 'Rubilicious'

begin
        unless ARGV.size == 5
                $stderr.puts "Usage: $0 [bloguser] [blogpass] [deluser] [delpass] [days_back]"
                exit -1
        end
        bloguser,blogpass,deluser,delpass,days_back= ARGV

        # Gather Links
        links_date = (Time.now - (86400 * days_back.to_i))
        r = Rubilicious.new(deluser,delpass)
        content = "<ul>"
        r.posts(links_date.strftime("%Y-%m-%d")).each do |link|
                content += "<li><a href=\"#{link['href']}\">#{link['href']}</a>"
                content += "<ul>"
                content += "<li>Description: #{link['description']}</li>"
                content += "<li>#{link['extended']}</li>"
                content += "<li>Posted: #{link['time']}</li>"
                content += "</ul></li>"
        end
        content += "</ul>"

        content += "<p>Click <a href=\"http://del.icio.us/mocephus/\">here</a> for all of my del.icio.us bookmarks."

        # Post Links To Blog Via XMLRPC
        server = XMLRPC::Client.new( "cracker.mocephus.org", "/xmlrpc.php")
        newPost = Hash.new
        newPost['title'] = "del.icio.us bookmarks - #{links_date.strftime('%m/%d/%Y')}"
        newPost['description'] = content
        result = server.call("metaWeblog.newPost",1,bloguser,blogpass,newPost,true)

rescue XMLRPC::FaultException => e
        puts "XMLRPC Error: "
        puts e.faultCode
        puts e.faultString
rescue StandardError => e
        puts e.to_s
end

Wordpress clean URLS with Lighttpd, the E-Z way

This single line seems to work for my WP install. I needed to add a couple of redirects and rewrites to handle my RSS feed, since I use Feedburner, but this line does all the heavy lifting...

Place it after server.document-root, either in the main section or in your virtual host sub-sections...

server.error-handler-404 = "/index.php?error=404"


« Newer Snippets
Older Snippets »
9 total  XML / RSS feed