Created
July 5, 2018 12:55
-
-
Save ChrisHardie/39c48cfe075bc920eac97813b8b83ae9 to your computer and use it in GitHub Desktop.
Extend the Jetpack Portfolio CPT to allow specifying a project URL that can be linked to instead of the portfolio item itself
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class JCH_Portfolio_Postmeta | |
* Create meta boxes, post meta fields and archive link filters for portfolio items | |
* With some help from https://gist.github.com/carlodaniele/121841f92956c3304436 | |
*/ | |
class JCH_Portfolio_Postmeta { | |
public function __construct() { | |
add_action( 'init', array( $this, 'init' ), 1 ); | |
} | |
public function init() { | |
add_action( 'add_meta_boxes_jetpack-portfolio', array( $this, 'portfolio_add_meta_boxes' ) ); | |
add_action( 'save_post_jetpack-portfolio', array( $this, 'portfolio_save_meta_box_data' ) ); | |
add_filter( 'post_type_link', array( $this, 'portfolio_permalink' ) ); | |
} | |
/** | |
* Add meta box | |
* | |
* @param post $post The post object | |
* @link https://codex.wordpress.org/Plugin_API/Action_Reference/add_meta_boxes | |
*/ | |
public function portfolio_add_meta_boxes( $post ) { | |
add_meta_box( | |
'portfolio_meta_box', | |
'Project URL', | |
array( $this, 'portfolio_build_meta_box' ), | |
'jetpack-portfolio', | |
'normal', | |
'high' | |
); | |
} | |
/** | |
* Build custom field meta box | |
* | |
* @param post $post The post object | |
*/ | |
public function portfolio_build_meta_box( $post ) { | |
// make sure the form request comes from WordPress | |
wp_nonce_field( basename( __FILE__ ), 'portfolio_meta_box_nonce' ); | |
// retrieve the _project_url current value | |
$current_project_url = get_post_meta( $post->ID, '_project_url', true ); | |
?> | |
<div class='inside'> | |
<p>If this field is populated with a URL, the archive display of the item will link directly to the URL.</p> | |
<p> | |
<input type="text" size="60" name="project_url" title="Project URL" value="<?php echo esc_url( $current_project_url ); ?>"/> | |
</p> | |
</div> | |
<?php | |
} | |
/** | |
* Store custom field meta box data | |
* | |
* @param int $post_id The post ID. | |
* @link https://codex.wordpress.org/Plugin_API/Action_Reference/save_post | |
*/ | |
public function portfolio_save_meta_box_data( $post_id ) { | |
// verify meta box nonce | |
if ( ! isset( $_POST['portfolio_meta_box_nonce'] ) || ! wp_verify_nonce( $_POST['portfolio_meta_box_nonce'], basename( __FILE__ ) ) ) { | |
return; | |
} | |
// return if autosave | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { | |
return; | |
} | |
// Check the user's permissions. | |
if ( ! current_user_can( 'edit_post', $post_id ) ) { | |
return; | |
} | |
// store custom fields values | |
// project_url | |
if ( isset( $_REQUEST['project_url'] ) ) { | |
update_post_meta( $post_id, '_project_url', sanitize_text_field( $_POST['project_url'] ) ); | |
} | |
} | |
/** | |
* For portfolio items, filter the permalink to go directly to the project URL if one exists | |
* @param $permalink | |
* @return string | |
*/ | |
public function portfolio_permalink( $permalink ) { | |
if ( 'jetpack-portfolio' === get_post_type() ) { | |
$project_url = get_post_meta( get_the_ID(), '_project_url', true ); | |
if ( ! empty( $project_url ) && wp_http_validate_url( $project_url ) ) { | |
return esc_url( $project_url ); | |
} | |
} | |
return $permalink; | |
} | |
} | |
$jch_portfolio_postmeta = new JCH_Portfolio_Postmeta(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment