Skip to content

Instantly share code, notes, and snippets.

@davilera
Last active October 21, 2015 10:13
Show Gist options
  • Save davilera/2bc7ec4515dbc814d137 to your computer and use it in GitHub Desktop.
Save davilera/2bc7ec4515dbc814d137 to your computer and use it in GitHub Desktop.

This gist contains the source code examples I use in this post.

<?php
/**
* Plugin Name: Nelio Nice Title
* Plugin URI: http://neliosoftware.com
* Description: This plugin tells all your visitors they're cool.
* Version: 1.0.0
* Author: David Aguilera
* Author URI: http://neliosoftware.com
* Requires at least: 4.0
* Tested up to: 4.3
*
* Text Domain: nelio-nice-title
* Domain Path: /languages/
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
add_filter( 'the_title', 'nelio_change_title', 10, 2 );
function nelio_change_title( $title, $id ) {
$title = $title . '&mdash;You\'re cool!';
return $title;
}
<?php
add_action( 'add_meta_boxes_post', 'nelio_add_meta_boxes' );
function nelio_add_meta_boxes() {
add_meta_box(
'nelio-nice-title',
'Nice Title',
'nelio_print_nice_title_meta_box'
);
}
function nelio_print_nice_title_meta_box( $post ) {
// No content...
}
<?php
function nelio_print_nice_title_meta_box( $post ) { ?>
<label for="nelio-nice-title">Text:</label>
<input name="nelio-nice-title" type="text" />
<?php
}
<?php
add_action( 'save_post', 'nelio_save_nice_title' );
function nelio_save_nice_title( $post_id ) {
// If we're auto-saving the post (post revisions), do nothing.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// If our text field is not set, do nothing.
if ( ! isset( $_REQUEST['nelio-nice-title'] ) ) {
return;
}
// If everything's OK, we get the data the user wrote, we clean it...
$nice_title = trim( sanitize_text_field( $_REQUEST['nelio-nice-title'] ) );
// ...and we save it as a post meta.
update_post_meta( $post_id, '_nelio_nice_title', $nice_title );
}
<?php
function nelio_print_nice_title_meta_box( $post ) {
$post_id = $post->ID;
$nice_title = get_post_meta( $post_id, '_nelio_nice_title', true ); ?>
<label for="nelio-nice-title">Text:</label>
<input name="nelio-nice-title" type="text" value="<?php
echo esc_attr( $nice_title );
?>" />
<?php
}
<?php
function nelio_change_title( $title, $id ) {
$nice_title = get_post_meta( $id, '_nelio_nice_title', true );
if ( ! empty( $nice_title ) ) {
$title = $title . ' ' . $nice_title;
}
return $title;
}
<?php
/**
* Plugin Name: Nelio Nice Title
* Plugin URI: http://neliosoftware.com
* Description: This plugin tells all your visitors they're cool.
* Version: 1.0.0
* Author: David Aguilera
* Author URI: http://neliosoftware.com
* Requires at least: 4.0
* Tested up to: 4.3
*
* Text Domain: nelio-nice-title
* Domain Path: /languages/
*/
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
// =============================================================
// Front-end
// =============================================================
add_filter( 'the_title', 'nelio_change_title', 10, 2 );
function nelio_change_title( $title, $id ) {
$nice_title = get_post_meta( $id, '_nelio_nice_title', true );
if ( ! empty( $nice_title ) ) {
$title = $title . ' ' . $nice_title;
}
return $title;
}
// =============================================================
// Meta Box
// =============================================================
add_action( 'add_meta_boxes_post', 'nelio_add_meta_boxes' );
function nelio_add_meta_boxes() {
add_meta_box(
'nelio-nice-title',
'Nice Title',
'nelio_print_nice_title_meta_box'
);
}
function nelio_print_nice_title_meta_box( $post ) {
$post_id = $post->ID;
$nice_title = get_post_meta( $post_id, '_nelio_nice_title', true ); ?>
<label for="nelio-nice-title">Text:</label>
<input name="nelio-nice-title" type="text" value="<?php
echo esc_attr( $nice_title );
?>" />
<?php
}
// =============================================================
// Saving the post meta
// =============================================================
add_action( 'save_post', 'nelio_save_nice_title' );
function nelio_save_nice_title( $post_id ) {
// If we're auto-saving the post (post revisions), do nothing.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// If our text field is not set, do nothing.
if ( ! isset( $_REQUEST['nelio-nice-title'] ) ) {
return;
}
// If everything's OK, we get the data the user wrote, we clean it...
$nice_title = trim( sanitize_text_field( $_REQUEST['nelio-nice-title'] ) );
// ...and we save it as a post meta.
update_post_meta( $post_id, '_nelio_nice_title', $nice_title );
}
<?php
// ...
class Nelio_Nice_Title_Public {
// ...
/**
* Modifies the title of the given post and appends Nelio's Nice Title (if any).
*
* @param string $title the post title.
* @param int $id the ID of the post.
*
* @return string the original title with Nelio's Nice Title appended (if any).
*
* @since 1.0.0
*/
public function append_nice_title( $title, $id ) {
$nice_title = get_post_meta( $id, '_nelio_nice_title', true );
if ( ! empty( $nice_title ) ) {
$title = $title . '&mdash;' . $nice_title;
}
return $title;
}
}
<?php
// ...
class Nelio_Nice_Title {
// ...
/**
* Register all of the hooks related to the public-facing functionality
* of the plugin.
*
* @since 1.0.0
* @access private
*/
private function define_public_hooks() {
$plugin_public = new Nelio_Nice_Title_Public( $this->get_plugin_name(), $this->get_version() );
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );
// Append Nelio's Nice Title
$this->loader->add_filter( 'the_title', $plugin_public, 'append_nice_title', 10, 2 );
}
// ...
}
<?php
/**
* This class is responsible of the Nice Title meta box.
*
* In particular, it's responsible of displaying the meta box and
* saving the value(s) its form contains.
*
* @package Nelio_Nice_Title
* @subpackage Nelio_Nice_Title/admin
* @author Your Name <email@example.com>
*/
class Nelio_Nice_Title_Meta_Box {
/**
* Displays the meta box.
*
* @param WP_Post $post The object for the current post/page.
*
* @since 1.0.0
*/
public function display( $post ) {
$post_id = $post->ID;
$nice_title = get_post_meta( $post_id, '_nelio_nice_title', true );
include plugin_dir_path( __FILE__ ) . 'partials/nelio-nice-title-meta-box.templ.php';
}
/**
* Registers the meta box and makes it available in the Post Editor display.
*
* @since 1.0.0
*/
public function register() {
add_meta_box(
'nelio-nice-title',
'Nice Title',
array( $this, 'display' )
);
}
/**
* Saves all the fields displayed in the meta box.
*
* @param int $post_id The ID of the post that's about to be saved.
*
* @since 1.0.0
*/
public function save( $post_id ) {
// If we're auto-saving the post (post revisions), do nothing.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// If our text field is not set, do nothing.
if ( ! isset( $_REQUEST['nelio-nice-title'] ) ) {
return;
}
// If everything's OK, we get the data the user wrote, we clean it...
$nice_title = trim( sanitize_text_field( $_REQUEST['nelio-nice-title'] ) );
// ...and we save it as a post meta.
update_post_meta( $post_id, '_nelio_nice_title', $nice_title );
}
}
<?php
/**
* The content of a Nelio Nice Title metabox, which is used in the Post Editor screen.
*
* This template assumes that the following variable is defined:
* * $nice_title {string} the Nice Title that has to be appended to the post (if any).
*
* @link http://example.com
* @since 1.0.0
*
* @package Nelio_Nice_Title
* @subpackage Nelio_Nice_Title/admin/partials
*/
?>
<label for="nelio-nice-title">Text:</label>
<input name="nelio-nice-title" type="text" value="<?php
echo esc_attr( $nice_title );
?>" />
<?php
// ...
class Nelio_Nice_Title {
// ...
/**
* Register all of the hooks related to the admin area functionality
* of the plugin.
*
* @since 1.0.0
* @access private
*/
private function define_admin_hooks() {
$plugin_admin = new Nelio_Nice_Title_Admin( $this->get_plugin_name(), $this->get_version() );
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
// Load the meta box class and create its hooks:
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-nelio-nice-title-meta-box.php';
$meta_box = new Nelio_Nice_Title_Meta_Box();
$this->loader->add_action( 'add_meta_boxes_post', $meta_box, 'register' );
$this->loader->add_action( 'save_post', $meta_box, 'save' );
}
// ...
}
<?php
if($aux1 && !empty($aux2 ))
{ nelio_update( $post,$aux2);
nelio_ok();
}
else{ nelio_error(2);}
<?php
if ( $is_data_valid && !empty( $name ) ) {
nelio_update_post_name( $post, $name );
nelio_print_success_message();
} else {
nelio_print_error_message( NELIO_INVALID_DATA_ERR );
}
<?php
function addition( $a, $b ) {
// Adds $a and $b
$c = $a + $b;
// Return the addition of $a and $b
return $c;
}
/**
* This function renders the whole collection.
*
* For each element in the collection, its own view is created,
* rendered, and appended to this parent view.
*
* @return {AccountListView} this instance.
*/
render: function() {
// First, we trigger a "render" event so that old child views can be removed.
this.trigger( 'nelioab:render' );
// Let's prepare the new view for the collection.
this.$el.html( this.template() );
var list = this.$( '.list' );
this.collection.each( function( action ) {
// We create a view to display it and we append it to the list.
var view = new ActionView( action );
list.append( view.render().el );
// And we listen to the render event. This way, when the
// collection's view is about to be re-rendered, this view
// will be closed and removed (preventing memory leaks).
view.listenTo( this, 'nelioab:render', view.close );
}
},
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment