Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Lets you add custom fields to a WordPress post or page via shortcodes. This allows you to use the WordPress mobile app to update custom fields. Currently working on this for a product release and wanted to share.
<?php
/*
Plugin Name: Custom Field Shortcodes
Version: 0.0.1
Plugin URI: https://gist.github.com/joshcanhelp/87e1bb70f97475bccc9eb123e3fe2f1c
Description: Lets you add custom fields to a WordPress post or page via shortcodes.
Author: Josh Can Help
Author URI: http://www.joshcanhelp.com/
License: GPL v3
*/
class CustomFieldShortcodes {
public $shortcode = '';
public $post_types = array();
/**
* WpdShortcodeFields constructor.
*
* @param string $shortcode - Shortcode used, like [sc_fields]
* @param array $post_types- What post types should it operate on?
*/
public function __construct( $shortcode, $post_types ) {
// Set the class shortcode to look for
$this->shortcode = $shortcode;
// Add the shortcode with the constructor param
add_shortcode( $shortcode, array( $this, 'shortcode' ) );
// Set the post types to use
$this->post_types = $post_types;
// Add the save_post processing
add_action( 'save_post', array( $this, 'save_post' ), 100, 2 );
}
/**
* Declaring the shortcode so it's not inadvertently shown
*
* @return string
*/
public function shortcode() {
return '';
}
/**
* Process shortcodes to save WPD fields from the mobile app
* Formatted like: [shortcode url=http://www.joshcanhelp.com featured=yes]
*
* @param int $post_id
* @param WP_Post $post
*/
public function save_post( $post_id, $post ) {
// Not needed for revisions
if ( wp_is_post_revision( $post_id ) ) {
return;
}
// Not a post type we're working with
if ( ! in_array( $post->post_type, $this->post_types ) ) {
return;
}
$matches = array();
preg_match_all( '/' . get_shortcode_regex() . '/s', $post->post_content, $matches );
// No fields to process, exit ...
if ( empty( $matches[2] ) || ! in_array( $this->shortcode, $matches[2] ) ) {
return;
}
// Look for the first shortcode
$sc_index = NULL;
$shortcode_start = '[' . $this->shortcode . ' ';
foreach ( $matches[0] as $index => $poss_match ) {
if ( $shortcode_start === substr( $poss_match, 0, strlen( $shortcode_start ) ) ) {
$sc_index = $index;
}
}
// Got nothing, exit ...
if ( is_null( $sc_index ) ) {
return;
}
$new_content = $post->post_content;
/*
* Looking for attributes and storing as needed
*/
if ( ! empty( $matches[3][ $sc_index ] ) ) {
$sc_attrs = explode( ' ', $matches[3][ $sc_index ] );
foreach ( $sc_attrs as $sc_attr ) {
if ( substr( $sc_attr, 0, strlen( 'url=' ) ) === 'url=' ) {
// Takes a "url" attribute and stores it as a custom field
$outbound_link = substr( $sc_attr, strlen( 'url=' ) );
$outbound_link = str_replace( '"', '', $outbound_link );
$outbound_link = esc_url( $outbound_link );
update_post_meta( $post_id, 'link', $outbound_link );
} else if ( substr( $sc_attr, 0, strlen( 'featured=' ) ) === 'featured=' ) {
// Takes a "featured" attribute and stores it as a true/false
$is_featured = substr( $sc_attr, strlen( 'featured=' ) );
$is_featured = str_replace( '"', '', $is_featured );
if ( 1 == $is_featured || 'yes' === $is_featured ) {
update_post_meta( $post_id, 'featured', 'yes' );
} elseif ( 0 == $is_featured || 'no' === $is_featured ) {
delete_post_meta( $post_id, 'featured' );
}
}
/*
* Add new attribute checks here
*/
}
}
// Shortcode content
if ( ! empty( $matches[5][ $sc_index ] ) ) {
$blurb = $matches[5][ $sc_index ];
update_post_meta( $post_id, 'blurb', wp_kses( $blurb, wp_kses_allowed_html() ) );
}
// Remove all the shortcodes found
foreach ( $matches[0] as $index => $match ) {
if ( $this->shortcode === $matches[2][ $index ] ) {
$new_content = str_replace( $match, '', $new_content );
}
}
// Avoid infinite loops with this hook
remove_action( 'save_post', array( $this, 'save_post' ), 100 );
// Skip default WP-Drudge processing
remove_action( 'save_post', 'wpd_save_metas', 10 );
// Update cleaned content
wp_update_post(
array(
'ID' => $post_id,
'post_content' => trim( $new_content ),
)
);
add_action( 'save_post', array( $this, 'save_post' ), 100, 2 );
add_action( 'save_post', 'wpd_save_metas', 10 );
}
}
new CustomFieldShortcodes( 'wpdrudge', array( 'post' ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment