Oct. 27, 2011

WordPress plugin: include custom fields in a post

This plugin lets you include custom fields inside a post, page, or text widget. It creates a shortcode that lets you include RAW HTML inside posts from the post's custom fields.

Where is this useful?

  • Advanced / power users might find this handy when visual editor mangles their HTML and custom markup.
  • Trusted editors might need to include non-standard embeds (third party iframe embeds, etc.)
  • If you need to include a site-wide pseudo-widget (repeating fact box, ad), this can work.
  • You can display custom fields in a widget (enable feature in options first.)

Usage Example

Create a custom field in the post

(ex: "my table" or "table"):

Then, inside a WP post put this shortcode:

[include table] or [include "my table"]

If needed, you can perform global includes from other posts with: [include global="my table"]

Recursive

Warning/extra feature: shortcodes can be recursive on some versions of WP. If you have these custom fields:

NameValue
OneFirst sentence.
TwoSecond sentence.
ThreeTest ing.

Putting [include three] in a WP post might produce:

"Test First sentence. Second sentence. ing."

Integrate external programs

Any program with MySQL access can thus be easily integrated via custom fields. Here's a tiny program that takes STDIN and dumps it into a custom field. Simply save this as wordpress-meta.php, chmod +x wordpress-meta.php and (after you make sure wp-config.php is available to it) you can do the following:

uptime | ./wordpress-meta.php "Machine Uptime" Will look for any posts with "Machine Uptime" meta key, and update it with readout from uptime command.
perl someprogram.pl | ./wordpress-meta.php 1012 special Takes input from a perl program, and puts it in post ID # 1012, meta key "special"

(You can download this utility from github)

#!/usr/bin/php
<?php
require( "wp-config.php" );

if( 
$argc == ) {
echo 
"\nThis program redirects STDIN into a WordPress custom field.\n";
echo 
"If a post ID is not specified, program will attempt to find\n";
echo 
"a matching post with a unique key (make sure key is unique)\n\n";
echo 
"\nUsage: ls | ./wordpress-meta.php [post_ID] \"[post meta name]\"\n\n";
echo 
"\tWhere post_ID is a number, and meta name is a key\n";
echo 
"\tIf meta name/key is a complex string, put it in quotes\n";
echo 
"\t\tex: cat /ads/current.txt | ./wordpress-meta.php \"global ads\"\n";
echo 
"\t\tex: uptime | ./wordpress-meta.php 1433 uptime\n\n";
die;
}

if( 
$argc )
    die( 
"Error: too many args. Try putting custom field name in quotes." );

if( 
$argc == && is_numeric($argv[1]) )
    die( 
"Error: custom field name missing, only got a number.\n" );

// assume no error after this point :/
if( $argc == ) {
    
$meta_key $argv[1];
    
$query = new WP_Query(array(
        
'post_type' => 'any',
        
'post_status' => 'any',
        
'posts_per_page' => 1,
        
'meta_key' => $meta_key
    
));
    if( empty( 
$query->posts ) ) {
        die( 
"Post not found with that meta key\n" );
    }
    
$post_id $query->posts[0]->ID;
} elseif( 
$argc == ) {
    if( 
is_numeric($argv[1]) ) {
        
$post_id $argv[1];
        
$meta_key $argv[2];
    } else {
        
$post_id $argv[2];
        
$meta_key $argv[1];
    }
}

$text '';
$fp fopen'php://stdin''r');
while( !
feof$fp ) ) $text .= fgets$fp4096 );
fclose$fp );

update_post_meta$post_id$meta_key$text );

printf(
    
"Updated, post id = %s, meta_key='%s', length=%s\n",
    
$post_id$meta_keystrlen$text )
); 

?>

Download

Get the WP plugin from https://github.com/pp19dd/Include-custom-field

 

Posts on this blog solely represent my personal opinions and technical experience.

© 2009-2014 Edin (Dino) Beslagic