Created
September 2, 2016 07:35
-
-
Save benthebear/34354964f5d61fcca42a4ca1540fa5c5 to your computer and use it in GitHub Desktop.
Waves and Rocks Plugin
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 | |
/** | |
* Plugin Name: Waves and Rocks | |
* Description: A Plugin with several Features for my Blog | |
* Version: 0.9i | |
* Author: Benjamin Birkenhake | |
* Author URI: http://ben.grim.rocks | |
*/ | |
/* | |
# Table of Contents | |
1. All the Hooks for Actions and Filters | |
2. Admin Field for Custom CSS per Post/Page | |
3. Allow Overwrite of first Term-Pages by Pages | |
*/ | |
// Add Meta-Boxes | |
add_action( 'add_meta_boxes', 'waves_and_rocks_add_custom_meta_boxes' ); | |
// Save CSS Box | |
add_action( 'save_post', 'waves_and_rocks_save_meta_boxes' ); | |
// Add CSS to the Content | |
add_filter( 'the_content', 'waves_and_rocks_custom_css_the_content_filter' ); | |
// Add Meta Box to Term-Pages | |
add_action( 'edit_category_form', 'waves_and_rocks_add_page_for_term_box' ); | |
add_action( 'edit_tag_form', 'waves_and_rocks_add_page_for_term_box' ); | |
add_action( 'edit_terms', 'waves_and_rocks_save_page_for_term', 10, 2 ); | |
// Render the Page within the Term-Description | |
add_action( 'term_description', 'waves_and_rocks_term_overwrite' ); | |
// Redirect Pages, that are rewrites for Terms | |
add_action( 'get_header', 'waves_and_rocks_header' ); | |
// Some Debugging-Stuff | |
add_action('admin_footer', 'waves_and_rocks_findout_screen'); | |
// Add Link to Page Edit | |
add_action('admin_bar_menu', 'waves_and_rocks_admin_bar_menu', 90); | |
/* ## MetaBox for CSS */ | |
function waves_and_rocks_add_custom_meta_boxes( $post ) { | |
add_meta_box( | |
'waves-and-rocks-meta-box-custom-css', | |
__( 'Custom CSS' ), | |
'waves_and_rocks_render_meta_box_custom_css', | |
array('post', 'page'), | |
'normal', | |
'default' | |
); | |
} | |
function waves_and_rocks_render_meta_box_custom_css($post){ | |
// Add a nonce field so we can check for it later. | |
wp_nonce_field( 'waves_and_rocks_save_meta_box_custom_css_data', 'waves_and_rocks_meta_box_custom_css_nonce' ); | |
echo '<textarea name="waves-and-rocks-meta-box-custom-css" style="with:100%;" cols="80" rows="10">'; | |
echo get_post_meta( $post->ID, "_waves-and-rocks-custom-css", true ); | |
echo '</textarea>'; | |
} | |
function waves_and_rocks_save_meta_boxes($post_id){ | |
// Verify that the nonce is valid. | |
if ( ! wp_verify_nonce( $_POST['waves_and_rocks_meta_box_custom_css_nonce'], 'waves_and_rocks_save_meta_box_custom_css_data' ) ) { | |
return; | |
} | |
if ( ! wp_verify_nonce( $_POST['waves_and_rocks_meta_box_page_for_term_nonce'], 'waves_and_rocks_save_meta_box_page_for_term' ) ) { | |
return; | |
} | |
// If this is an autosave, our form has not been submitted, so we don't want to do anything. | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { | |
return; | |
} | |
// Check the user's permissions. | |
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) { | |
if ( ! current_user_can( 'edit_page', $post_id ) ) { | |
return; | |
} | |
} else { | |
if ( ! current_user_can( 'edit_post', $post_id ) ) { | |
return; | |
} | |
} | |
// Save CSS | |
if (isset( $_POST['waves-and-rocks-meta-box-custom-css'] ) ) { | |
// Sanitize user input. | |
$my_data = $_POST['waves-and-rocks-meta-box-custom-css']; | |
// Update the meta field in the database. | |
update_post_meta( $post_id, '_waves-and-rocks-custom-css', $my_data ); | |
} | |
} | |
function waves_and_rocks_custom_css_the_content_filter($content) { | |
$styles = get_post_meta( $GLOBALS['post']->ID, "_waves-and-rocks-custom-css", true ); | |
if (is_singular() and $styles !="") { | |
$output = ""; | |
$output .= "<style>"; | |
$output .= $styles; | |
$output .= "</style>"; | |
$content = $output.$content; | |
} | |
// otherwise returns the database content | |
return $content; | |
} | |
/* ## 2. Allow Overwrite of first Term-Pages by Pages */ | |
function waves_and_rocks_term_overwrite( $value ) { | |
$output = ""; | |
$found = false; | |
if(is_archive()){ | |
$output .= "<!-- … is archive -->"; | |
global $wp_query; | |
if(isset($wp_query->query_vars['paged']) and $wp_query->query_vars['paged'] == 0){ | |
$output .= "<!-- … is first page -->"; | |
$term = get_queried_object(); | |
//print_r($term); | |
if(isset($term->term_id)){ | |
$post_id = _waves_and_rocks_term_is_overwritten_by($term->term_id); | |
if(isset($post_id) and is_numeric($post_id)){ | |
$post = get_post($post_id); | |
if(isset($post->post_content)){ | |
$found = true; | |
if(has_post_thumbnail($post->ID)){ | |
$output .= "<p class='teaserimage'>"; | |
$output .= get_the_post_thumbnail($post->ID, 'medium'); | |
$output .= "</p>"; | |
} | |
$output .= $post->post_content; | |
$output .= "<p><a href='".get_edit_post_link($post->ID)."'>".__("Edit Page")."</a></p>"; | |
$output .= "<div class='broadside'><hr/></div>"; | |
$output .= "<h2>".__('Alle Beiträge …')."</h2>" ; | |
} | |
} | |
} | |
} | |
} | |
// Fallback | |
if(!$found){ | |
$output = $value; | |
} | |
return $output; | |
} | |
function waves_and_rocks_add_page_for_term_box($object){ | |
$page_id = get_term_meta($object->term_id, 'waves_and_rocks_page_for_term', true); | |
print "<table class='form-table'>"; | |
print "<tbody>"; | |
print "<tr class='form-field'>"; | |
print "<th scope='row'>"; | |
print "<label for='page_for_term'>".__('Page for this Term')."</label>"; | |
print "</th>"; | |
print "<td>"; | |
print "<input id='waves_and_rocks_page_for_term' name='waves_and_rocks_page_for_term' value='".$page_id."' size='40' type='text'/>"; | |
print "<p class='description'>".__('Choose a Page for this Term')."</p>"; | |
print "</td>"; | |
print "</tr>"; | |
print "</tbody>"; | |
print "</table>"; | |
} | |
function waves_and_rocks_save_page_for_term($term_id, $taxonomy){ | |
if(isset($_POST['waves_and_rocks_page_for_term'])){ | |
update_term_meta($term_id, 'waves_and_rocks_page_for_term', $_POST['waves_and_rocks_page_for_term']); | |
} | |
} | |
function _waves_and_rocks_term_is_overwritten_by($term_id){ | |
global $wpdb; | |
$sql = "SELECT meta_value FROM ".$wpdb->termmeta." WHERE term_id = '".$term_id."' and meta_key = 'waves_and_rocks_page_for_term'"; | |
$result = $wpdb->get_var($sql); | |
return $result; | |
} | |
function _waves_and_rocks_page_is_overwrite_for($page_id){ | |
global $wpdb; | |
$sql = "SELECT term_id FROM ".$wpdb->termmeta." WHERE meta_value = '".$page_id."' and meta_key = 'waves_and_rocks_page_for_term'"; | |
$result = $wpdb->get_var($sql); | |
return $result; | |
} | |
function waves_and_rocks_findout_screen(){ | |
if(function_exists("get_current_screen")){ | |
print "\n<!-- current_screen: [".get_current_screen()->id."] -->\n\n"; | |
} | |
} | |
// Redirect Pages to their related Term | |
function waves_and_rocks_header($name){ | |
if(is_page()){ | |
$page = get_queried_object(); | |
if(isset($page->ID)){ | |
$term_id = _waves_and_rocks_page_is_overwrite_for($page->ID); | |
if(is_numeric($term_id)){ | |
$term_object = get_term($term_id); | |
//print_r($term_object); | |
$term_link = get_term_link($term_object); | |
//print $term_link; | |
if($term_link and strpos($term_link, "http://") === 0){ | |
wp_redirect($term_link); | |
} | |
} | |
} | |
} | |
} | |
// Add an Edit-Page Button to the Admin Bar, if on a Term page that has a Page | |
function waves_and_rocks_admin_bar_menu(){ | |
global $wp_admin_bar; | |
$term = get_queried_object(); | |
$page_id = _waves_and_rocks_term_is_overwritten_by($term->term_id); | |
if((is_category() or is_tag()) and is_numeric($page_id)){ | |
$menu = array(); | |
$menu['id'] = 'wp-edit-page-replacing-term'; | |
$menu['title'] = __('Edit Page'); | |
$menu['meta']['class'] = 'ab-item'; | |
$menu['href'] = get_edit_post_link($page_id); | |
} | |
$wp_admin_bar->add_menu($menu); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment