Instantly share code, notes, and snippets.
Created
July 16, 2015 21:49
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save rveitch/9706b2c6712d6cb9459a to your computer and use it in GitHub Desktop.
"FCC Open Byline Authors" plugin for WordPress.
This file contains 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 | |
/* | |
Plugin name: FCC Open Byline Authors | |
Plugin URI: http://www.forumcomm.com/digital-network/ | |
Description: This plugin adds options to assign alternate post authors as bylines without creating WordPress user accounts. The selected author name and link will be shown on posts in place of the original post author. If no "Open Byline Author" is assigned to a post, the original post author will be use. | |
Author: FCC (Ryan Veitch) | |
Author URI: http://www.forumcomm.com/digital-network/ | |
Version: 1.0 | |
*/ | |
/*************************** Plugin Registration ***************************** | |
******************************************************************************* | |
* Plugin loading, registration and admin dashboard menu functions. | |
*/ | |
// Call fcc_open_byline_menu function to load plugin menu in dashboard | |
add_action( 'admin_menu', 'fcc_open_byline_menu' ); | |
// Create WordPress admin menu | |
function fcc_open_byline_menu(){ | |
$page_title = 'Open Byline'; | |
$menu_title = 'Open Byline'; | |
$capability = 'manage_options'; | |
$menu_slug = 'fcc-open-byline-admin'; | |
$function = 'fcc_open_byline_page'; | |
$icon_url = 'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48c3ZnIHZlcnNpb249IjEuMiIgYmFzZVByb2ZpbGU9InRpbnkiIGlkPSJMYXllcl8xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiB4PSIwcHgiIHk9IjBweCIgd2lkdGg9IjUxMnB4IiBoZWlnaHQ9IjUxMnB4IiB2aWV3Qm94PSIwIDAgNTEyIDUxMiIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSI+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5Yy0xNC41MjQsOS43NS0xMS43NzYsNDQuOTMzLTExLjc3Niw2OS44MTVjMCwxNC41MjEtNC4wODEsNTguMTA0LDIuNTIzLDY4LjEzNGM2Ljk2OSwxMC41ODEsMjcuMjc0LDYuNjczLDQ1LjQyMiw3LjU2OWMyNi40NDktNTcuOTQ3LDcwLjYyMi05OC4xNjksMTMzLjc0NS0xMTkuNDQzQzI3Mi43MTcsMTE1LjA2NiwyNDUuMzM4LDEwOS42MzIsMjExLjQzNywxMDcuNDQ5eiBNMTA2LjI5MiwyODkuMTM5Yy03LjEwNCw5Ljc3OS0xLjY4MiwzNi43MDItMS42ODIsNTAuNDY5YzAsMzguNDIxLTUuNjU1LDk3LjQ0MywxNC4yOTksMTEzLjU1N2MxMi4wNTktMC41NiwyNi4zNTMsMS4xMTgsMzcuMDEtMC44NDJjLTIxLjMyMy00Mi4wNjQtMzEuNDI4LTExNS4wNjMtMTQuMjk5LTE2OS4wNzJDMTI5Ljc4NiwyODMuNjU1LDExMS45ODEsMjgxLjMwNywxMDYuMjkyLDI4OS4xMzl6Ii8+PC9zdmc+'; | |
add_menu_page( $page_title, | |
$menu_title, | |
$capability, | |
$menu_slug, | |
$function, | |
$icon_url, | |
$position ); | |
// Call update_fcc_open_byline function to update database | |
add_action( 'admin_init', 'update_fcc_open_byline' ); | |
} | |
// Create function to register plugin settings in the database | |
function update_fcc_open_byline() { | |
register_setting( 'fcc-open-byline-admin-settings', 'fcc_open_byline' ); | |
} | |
// Dashboard Plugin Page Content/Function | |
function fcc_open_byline_page(){ | |
?> | |
<h1>FCC Open Byline Authors Settings and Instructions</h1> | |
<hr> | |
<h2>All Registered "Open Byline Authors" Currently In Use:</h2> | |
<div> | |
<?php | |
$args = array( 'hide_empty=0' ); | |
$terms = get_terms( 'open_byline_author', $args ); | |
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { | |
$count = count( $terms ); | |
$i = 0; | |
$term_list = '<p class="open_byline_author">'; | |
foreach ( $terms as $term ) { | |
$i++; | |
$term_list .= '<a href="' . get_term_link( $term ) . '" title="' . sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) . '">' . $term->name . '</a>'; | |
if ( $count != $i ) { | |
$term_list .= ' · '; | |
} | |
else { | |
$term_list .= '</p>'; | |
} | |
} | |
echo $term_list; | |
} | |
?> | |
</div> | |
<hr> | |
<div class="card pressthis"> | |
<h3>About<h3> | |
<p>This plugin adds options to assign alternate post authors as bylines without creating WordPress user accounts. The selected author name and link will be shown on posts in place of the original post author. If no "Open Byline Author" is assigned to a post, the original post author will be use.</p> | |
</div> | |
<div class="card pressthis"> | |
<h3>Features Overview<h3> | |
<p> | |
Use the sidebar widget to:<br> | |
- Select author from the sidebar widget when writing or editing a post.<br> | |
- Add a new author without leaving the page.<br> | |
- Use the "Most Used" tab to quickly find frequently used authors. | |
</p> | |
<img src="<?php echo plugins_url( '/images/fcc-oba-edit-post.png', __FILE__ ); ?>" width="520"> | |
<hr> | |
<p> | |
- Add or edit new authors, or enter additional (optional) author information including author descriptions in the admin page under "Posts".<br> | |
- See the number of articles associated with each author in the "Count" column. | |
</p> | |
<img src="<?php echo plugins_url( '/images/fcc-oba-authors.png', __FILE__ ); ?>" width="520"> | |
<hr> | |
<p>Easily check post/author association on the "All Posts" page.</p> | |
<img src="<?php echo plugins_url( '/images/fcc-oba-posts.png', __FILE__ ); ?>" width="520"> | |
</div> | |
<?php | |
} | |
/** | |
* | |
* Plugin "Functions" Code Begins Here | |
* | |
*/ | |
/*************************** REGISTER THE TAXONOMY ***************************** | |
******************************************************************************* | |
* This registers a new "Open Byline Authors" taxonomy. Place in plugin or | |
* template functions.php. | |
*/ | |
add_action( 'init', 'register_oba_tax' ); | |
function register_oba_tax() { | |
$labels = array( | |
"name" => "Open Byline Authors", | |
"label" => "Open Byline Authors", | |
"menu_name" => "Open Byline Authors", | |
"all_items" => "All Open Byline Authors", | |
"edit_item" => "Edit Open Byline Author", | |
"view_item" => "View Open Byline Author", | |
"update_item" => "Update Open Byline Author", | |
"add_new_item" => "Add New Open Byline Author", | |
"new_item_name" => "New Open Byline Author Name", | |
"parent_item" => "Parent of Open Byline Author", | |
"parent_item_colon" => "Parent Open Byline Author:", | |
"search_items" => "Search Open Byline Author:", | |
"popular_items" => "Popular Open Byline Authors", | |
"separate_items_with_commas" => "Separate open byline authors with commas", | |
"add_or_remove_items" => "Add or remove open byline author", | |
"choose_from_most_used" => "Choose from the most used open byline authors", | |
"not_found" => "No open byline authors found", | |
); | |
$args = array( | |
"labels" => $labels, | |
"hierarchical" => true, | |
"label" => "Open Byline Authors", | |
"has_archive" => true, | |
"show_ui" => true, | |
"query_var" => true, | |
"rewrite" => array( 'slug' => 'open_byline_author', 'with_front' => true ), | |
"show_admin_column" => true, | |
); | |
register_taxonomy( "open_byline_author", array( "post" ), $args ); | |
} // End of Taxonomy Registration | |
/*************************** FILTER FUNCTIONS ********************************** | |
******************************************************************************* | |
* Filter functions begin here. | |
*/ | |
// Action to call the add_filter(s) function | |
add_action('wp_loaded', 'byline_function'); | |
function byline_function() { | |
add_filter( 'the_author', 'modify_author_name' ); | |
add_filter( 'author_link', 'modify_author_link', 10, 1 ); | |
} | |
// Author Name Replacement Function | |
function modify_author_name( $name ) { | |
global $post; | |
$author_name = get_the_author_meta('display_name'); | |
$author_ID = get_query_var('author'); | |
$author_url = get_author_posts_url( get_the_author_meta( $author_ID ) ); | |
$real_author = get_the_author_meta('display_name'); | |
$byline_terms = get_the_terms( $post->ID, 'open_byline_author' ); | |
if( !is_wp_error( $byline_terms ) && ( !empty( $byline_terms ) ) ) { | |
$byline_term = array_pop($byline_terms); | |
$byline_author = $byline_term->name; | |
$name = $byline_author; | |
return $name; | |
} | |
else { | |
$name = $real_author; | |
return $name; | |
} | |
} | |
// Author Link Replacement Function | |
function modify_author_link( $link ) { | |
global $post; | |
$byline_terms = get_the_terms( $post->ID, 'open_byline_author' ); | |
if( !is_wp_error( $byline_terms ) && ( !empty( $byline_terms ) ) ) { | |
$byline_term = array_pop($byline_terms); | |
$byline_author_link = '<a href="'.get_term_link($byline_term->slug, 'open_byline_author').'">'.$byline_term->name.'</a>'; | |
$link = get_term_link($byline_term->slug, 'open_byline_author'); | |
return $link; | |
} | |
else { | |
$link = $link; | |
return $link; | |
} | |
} | |
// End Plugin Code // | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment