Skip to content

Instantly share code, notes, and snippets.

@Shelob9
Last active April 30, 2017 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shelob9/b8c89487fc59f81a0f0f5e5f9a03d489 to your computer and use it in GitHub Desktop.
Save Shelob9/b8c89487fc59f81a0f0f5e5f9a03d489 to your computer and use it in GitHub Desktop.
<?php
class Add_CTA_To_Content {
/**
* Call To Action to add to post
*
* @var string
*/
protected $call_to_action;
/**
* Add_CTA_To_Content constructor.
*
* @param string $call_to_action Call To Action to add to post
*/
public function __construct( $call_to_action ){
$this->call_to_action = $call_to_action;
}
/**
* Add the filter
*/
public function add_hook(){
add_filter( 'the_content', [ $this, 'filter_content' ] );
}
/**
* Remove the filter
*/
public function remove_hook(){
remove_filter( 'the_content', [ $this, 'filter_content' ] );
}
/**
* Append CTA to post content
*
* @param string $content
*
* @return string
*/
public function filter_content( $content ){
return $content . $this->call_to_action;
}
}
<?php
add_action( 'save_post', 'save_cta', 10, 2 );
function save_cta( $id, $post ){
remove_action( 'save_post', 'save_cta' );
$appender = new Add_CTA_To_Content( get_option( 'saved_cta' ) );
$post->post_content = $appender->filter_content( $post->post_content );
wp_update_post( $post );
}
<?php
add_action( 'save_post', [ 'Term_Control_Hooks', 'save_post' ], 10, 2 );
class Term_Control_Hooks {
public static function save_post( $id, $post ){
if( ! empty( $_POST[ 'post_category' ] ) ){
$categories = $_POST[ 'post_category' ];
$control = new Term_Control_2( $post, $categories );
$control->update_categories();
}
}
}
<?php
add_action( 'admin_init', [ 'Term_Control_Hooks', 'admin_hooks' ] );
class Term_Control_Hooks {
public static function admin_hooks(){
add_action( 'save_post', [ __CLASS__, 'save_post' ], 10, 2 );
}
public static function save_post( $id, $post ){
if( ! empty( $_POST[ 'post_category' ] ) ){
$categories = $_POST[ 'post_category' ];
$control = new Term_Control_2( $post, $categories );
$control->update_categories();
}
}
}
<?php
add_action( 'admin_init', [ 'Term_Control_Hooks', 'admin_hooks' ] );
class Term_Control_Hooks {
public static function admin_hooks(){
add_action( 'save_post', [ __CLASS__, 'save_post' ], 10, 2 );
add_action( 'admin_enqueue_scripts', [ __CLASS__ , 'load_scripts' ] );
}
public static function save_post( $id, $post ){
if( ! empty( $_POST[ 'post_category' ] ) ){
$categories = $_POST[ 'post_category' ];
$control = new Term_Control_2( $post, $categories );
$control->update_categories();
}
}
public static function load_scripts(){
}
}
<?php
class Term_Control_1 {
/**
* Create object
*/
public function __construct(){
add_action( 'save_post', [ $this, 'save_post' ], 10 );
}
/**
* When post is updated, check if has category 7 or not and add or remove custom field
*
* @param $id
*/
public function save_post( $id ){
remove_action( 'save_post', [ $this, 'save_post' ] );
if( ! empty( $_POST[ 'post_category' ] ) && in_array( 7, $_POST[ 'post_category' ] ) ){
update_post_meta( $id, '_fancy_cats', 1 );
}else{
delete_post_meta( $id, '_fancy_cats', 1 );
}
}
}
new Term_Control_1();
<?php
add_action( 'save_post', 'my_update_post', 10, 2 );
function my_update_post( $id, $post ){
if( ! empty( $_POST[ 'post_category' ] ) ){
$categories = $_POST[ 'post_category' ];
$control = new Term_Control_2( $post, $categories );
$control->update_categories();
}
}
<?php
class Term_Control_2 {
/**
* @var array
*/
protected $categories;
/**
* @var WP_Post
*/
protected $post;
/**
* Category to check for
*
* @var int
*/
protected $category = 7;
/**
* Custom field to write
*
* @var string
*/
protected $field = '_fancy_cats';
/**
* Term_Control_2 constructor.
*
* @param WP_Post $post
* @param array $categories
*/
public function __construct( WP_Post $post, array $categories ){
$this->post = $post;
$this->categories = $categories;
}
/**
* Update categories
*/
public function update_categories(){
if( $this->has_category() ){
$this->add_field();
}else{
$this->remove_field();
}
}
/**
* Add the custom field
*/
public function add_field(){
update_post_meta( $this->post->ID, $this->field, 1 );
}
/**
* Remove the custom field
*/
public function remove_field(){
delete_post_meta( $this->post->ID, $this->field, 1 );
}
/**
* Check if post has categories
*
* @return bool
*/
protected function has_category(){
return in_array( $this->category, $this->categories );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment