Skip to content

Instantly share code, notes, and snippets.

@X-Raym
Last active January 16, 2018 01:50
Show Gist options
  • Save X-Raym/de352ced93a7844e32a4 to your computer and use it in GitHub Desktop.
Save X-Raym/de352ced93a7844e32a4 to your computer and use it in GitHub Desktop.
Site Specific WordPress Plugin Code Snippets
<?php
/*
Plugin Name: Site Plugin
Description: Site specific code
Author: X-Raym
Author URI: http://extremraym.com/
*/
/* Enable PHP in widgets */
add_filter('widget_text','execute_php',100);
function execute_php($html){
if(strpos($html,"<"."?php")!==false){
ob_start();
eval("?".">".$html);
$html=ob_get_contents();
ob_end_clean();
}
return $html;
}
// Enable shortcodes in widgets
add_filter('widget_text', 'do_shortcode');
// Désactiver Ping Interne
function nopinginterne( &$links ) {
$home = get_option( 'home' );
foreach ( $links as $l => $link )
if ( 0 === strpos( $link, $home ) )
unset($links[$l]);}
add_action( 'pre_ping', 'nopinginterne');
// Miniature dans le RSS
function wpc_rss_miniature($excerpt) {
global $post;
$content = '<p>' . get_the_post_thumbnail($post->ID) .
'</p>' . get_the_excerpt();
return $content;
}
add_filter('the_excerpt_rss', 'wpc_rss_miniature');
add_filter('the_content_feed', 'wpc_rss_miniature');
function my_theme_add_editor_styles() {
add_editor_style( 'custom-editor-style.css' );
}
add_action( 'after_setup_theme', 'my_theme_add_editor_styles' );
function override_mce_options($initArray) {
$opts = '*[*]';
$initArray['valid_elements'] = $opts;
$initArray['extended_valid_elements'] = $opts;
return $initArray;
}
add_filter('tiny_mce_before_init', 'override_mce_options');
function my_edit_toolbar($wp_toolbar) {
$wp_toolbar->remove_node('wp-logo');
$wp_toolbar->remove_node('site-name');
$wp_toolbar->remove_node('updates');
$wp_toolbar->remove_node('comments');
$wp_toolbar->remove_node('new-content');
$wp_toolbar->remove_node('top-secondary');
}
add_action('admin_bar_menu', 'my_edit_toolbar', 999);
// Add the new filter
add_filter('upload_mimes', 'addUploadMimes');
/**
* Adds new supported media types for upload.
*
* @see wp_check_filetype() or get_allowed_mime_types()
* @param array $mimes Array of mime types keyed by the file extension regex corresponding to those types.
* @return array
*/
function addUploadMimes($mimes)
{
$mimes = array_merge($mimes, array(
'ott|odt|zip|ReaperThemeZip|ReaperConfigZip' => 'application/octet-stream'
));
return $mimes;
}
function my_theme_add_editor_styles() {
add_editor_style( 'custom-editor-style.css' );
}
add_action( 'after_setup_theme', 'my_theme_add_editor_styles' );
/*body#tinymce.wp-editor{
font-family:"Open Sans","Helvetica Neue",Helvetica,Arial,"Lucida Grande",sans-serif;
margin:1em;
font-size:.875em;
line-height:1.5;
color:#666a76
}
body#tinymce.wp-editor a{
color:#53a1b8
}
*/
// Backend Thumbnail
add_filter('manage_posts_columns', 'posts_columns', 5);
add_action('manage_posts_custom_column', 'posts_custom_columns', 5, 2);
function posts_columns($defaults){
$defaults['riv_post_thumbs'] = __('Thumbs');
return $defaults;
}
function posts_custom_columns($column_name, $id){
if($column_name === 'riv_post_thumbs'){
echo the_post_thumbnail( 'featured-thumbnail' );
}
}
// Add admin CSS
function wptutsplus_admin_styles() {
wp_register_style( 'wptuts_admin_stylesheet', plugins_url( '/admin.css', __FILE__ ) );
wp_enqueue_style( 'wptuts_admin_stylesheet' );
}
add_action( 'admin_enqueue_scripts', 'wptutsplus_admin_styles' );
// Admin CSS
/*.meta-box-sortables h3, .postbox h3 {
background: none repeat scroll 0 0 #0F2142;
font-weight: normal;
color: #FFF!important;
}
.status-draft{background-color:#FFFF99 !important;}
.status-future{background-color:#ff999a !important;}
.status-publish{background-color:#CCFF99 !important;}
.status-pending{background-color:#87C5D6 !important;}
.status-private{background-color:#FFCC99 !important;}
*/
/**
* Automatic numbering for post title h2 -> h6
* Conditional load of a JS file
*/
if( !is_admin()){
wp_register_script('automatic_numbering_header', plugin_dir_url( __FILE__ ) . '/js/automatic-heading-number.js', array('jquery'), '1.8.0', true );
}
function organizedthemes_load_default_scripts() {
if( !is_admin()){
$auto = get_field('automatic_numbering');
if($auto == true)
{
wp_enqueue_script('automatic_numbering_header');
}
}
}
add_action('wp_enqueue_scripts', 'organizedthemes_load_default_scripts');
add_filter( 'jpeg_quality', create_function( '', 'return 100;' ) );
/**
* Disable WordPress Comments RSS ONLY
*
* @author Morality124
* @source https://wordpress.org/support/topic/how-to-disable-comments-feed-only?replies=1
*/
add_action( 'feed_link', 'remove_comments_feed', 10, 2 );
function remove_comments_feed( $output, $feed ) {
if ( strpos( $output, 'comments' ) )
return;
}
/*
*
* When define('WP_DEBUG', true); define('WP_DEBUG_LOG', true);
*/
if ( ! function_exists('write_log')) {
function write_log ( $log ) {
if ( is_array( $log ) || is_object( $log ) ) {
error_log( print_r( $log, true ) );
} else {
error_log( $log );
}
}
}
// Retirer les Headings HTML des Extraits
function bac_wp_strip_header_tags( $text ) {
$excerpt = $text;
$raw_excerpt = $text;
if ( '' == $text ) {
//Retrieve the post content.
$text = get_the_content('');
//remove shortcode tags from the given content.
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]&gt;', $text);
//Regular expression that strips the header tags and their content.
$regex = '#(<h([1-6])[^>]*>)\s?(.*)?\s?(<\/h\2>)#';
$text = preg_replace($regex,'', $text);
/***Change the excerpt word count.***/
$excerpt_word_count = 55; //This is WP default.
$excerpt_length = apply_filters('excerpt_length', $excerpt_word_count);
/*** Change the excerpt ending.***/
$excerpt_end = '[...]'; //This is the WP default.
$excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);
$excerpt = wp_trim_words( $text, $excerpt_length, $excerpt_more );
}
return apply_filters('wp_trim_excerpt', $excerpt, $raw_excerpt);
}
add_filter( 'get_the_excerpt', 'bac_wp_strip_header_tags', 5);
/**
* Add user profile link to list to Admin User Row
*/
function add_profile_link_user_row( $actions, $user_object ) {
$actions['profile_page'] = "<a href='" . get_author_posts_url( $user_object->ID ) . "'>" . __( 'View', 'site_plugin' ) . "</a>";
return $actions;
}
add_filter('user_row_actions', 'add_profile_link_user_row', 10, 2);
add_action( 'personal_options', 'action_personal_options', 2 );
/**
* Add user profile link to list in Admin Edit User Profile
*
* From http://wordpress.stackexchange.com/questions/69461/how-to-obtain-the-user-id-of-the-current-profile-being-edited-in-wp-admin
* @param WP_User $user User object for this screen.
*/
function action_personal_options( WP_User $user ) {
if ( defined('IS_PROFILE_PAGE') && IS_PROFILE_PAGE ) {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
// If is another user's profile page
} elseif (! empty($_GET['user_id']) && is_numeric($_GET['user_id']) ) {
$user_id = $_GET['user_id'];
// Otherwise something is wrong.
} else {
die( 'No user id defined.' );
}
?>
<tr>
<th class="view" scope="row"><?php _e( 'View Public Page', 'site_plugin' ); ?></th>
<td><a href="<?php echo get_author_posts_url( $user_id ); ?>"><?php _e( 'Public Page', 'site_plugin' ); ?></a></td>
</tr>
<?php
}
add_action( 'admin_bar_menu', 'user_profile_admin_bar_menu', 1 );
/**
* Adds a 'Switch back to {user}' link to the account menu in WordPress' admin bar.
*
* @param WP_Admin_Bar $wp_admin_bar The admin bar object
*/
function user_profile_admin_bar_menu( WP_Admin_Bar $wp_admin_bar ) {
if ( ! function_exists( 'is_admin_bar_showing' ) ) {
return;
}
if ( ! is_admin_bar_showing() ) {
return;
}
if ( method_exists( $wp_admin_bar, 'get_node' ) ) {
if ( $wp_admin_bar->get_node( 'user-actions' ) ) {
$parent = 'user-actions';
} else {
return;
}
} else if ( get_option( 'show_avatars' ) ) {
$parent = 'my-account-with-avatar';
} else {
$parent = 'my-account';
}
$url = get_author_posts_url( get_current_user_id() );
$wp_admin_bar->add_menu( array(
'parent' => $parent,
'id' => 'user_profile',
'title' => __( 'View My Public Profile', 'site_plugin' ),
'href' => $url,
) );
}
/**
* Add an Update anchor link that on edit screen admin pages to allow scroll to top of the Update metabox
*/
function add_update_item_admin_bar() {
$screen = get_current_screen();
if ( $screen->base == 'post' ) {
global $wp_admin_bar;
$wp_admin_bar->add_menu(array(
'title' => 'Update', // Titre du menu
'href' => "#pageparentdiv" // Lien du menu
));
}
}
add_action('admin_bar_menu', 'add_update_item_admin_bar', 999);
/*
Since v4
.wp-core-ui .button-primary {
text-shadow: none!important;
}
h2.ui-sortable-handle {
background: none repeat scroll 0 0 #262626;
font-weight: normal;
color: #FFF!important;
padding: 10px 10px 10px 25px;
}
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment