Skip to content

Instantly share code, notes, and snippets.

View wpscholar's full-sized avatar
😀
Happy

Micah Wood wpscholar

😀
Happy
View GitHub Profile
@wpscholar
wpscholar / wp-add-contact-method.php
Last active July 26, 2021 11:29
Add additional contact methods to a WordPress user profile
<?php
/**
* Add additional contact methods to a WordPress user profile
*/
function more_contactmethods( $contactmethods ) {
$contactmethods['twitter'] = 'Twitter username';
$contactmethods['facebook'] = 'Facebook URL';
return $contactmethods;
}
@wpscholar
wpscholar / wp-plugin-settings-link.php
Last active October 7, 2018 23:32
Add a settings link to your WordPress plugin
@wpscholar
wpscholar / wp-snapshot-shortcode.php
Last active May 20, 2020 15:51
Shortcode displays snapshots of remote sites on your WordPress site.
<?php
/**
* This shortcode will allow you to create a snapshot of a remote website and post it
* on your WordPress site.
*
* [snapshot url="http://www.wordpress.org" alt="WordPress.org" width="400" height="300"]
*/
add_shortcode( 'snapshot', function ( $atts ) {
$atts = shortcode_atts( array(
@wpscholar
wpscholar / members-only-shortcode.php
Last active September 20, 2017 23:32
WordPress shortcode will only display content to registered users.
<?php
/**
* WordPress shortcode only displays content to registered users. Could be improved by
* passing role as an attribute to only allow certain types of users.
*
* Example: [member]This text will be only displayed to registered users.[/member]
*/
function members_only_shortcode( $atts, $content = null ) {
if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
@wpscholar
wpscholar / load-pdf.php
Last active September 20, 2017 23:32
WordPress shortcode loads a PDF file on your site in an iframe using Google Docs
<?php
/**
* WordPress shortcode loads a PDF file on your site in an iframe using Google Docs.
*
* Example: [embed_pdf width="600px" height="500px"]http://infolab.stanford.edu/pub/papers/google.pdf[/embedpdf]
*/
function embed_pdf($attr, $url) {
return '<iframe src="http://docs.google.com/viewer?url=' . $url . '&embedded=true" style="width:' .$attr['width']. '; height:' .$attr['height']. ';" frameborder="0">Your browser should support iFrame to view this PDF document</iframe>';
}
@wpscholar
wpscholar / rss-shortcode.php
Last active September 20, 2017 23:32
WordPress shortcode allows you to create content that is visible only in your RSS feeds.
<?php
/**
* WordPress shortcode allows you to create content that is visible only in your RSS feeds.
*
* Example: [feedonly]Dear RSS readers...[/feedonly]
*/
function feedonly_shortcode( $atts, $content = null) {
if (!is_feed()) return "";
return $content;
@wpscholar
wpscholar / remove-admin-menu-items.php
Last active September 20, 2017 23:32
Remove unwanted pages from the WordPress admin menu
<?php
/**
* Remove unwanted pages from the WordPress admin menu
*/
function my_admin_menu() {
remove_menu_page('link-manager.php');
}
add_action( 'admin_menu', 'my_admin_menu' );
@wpscholar
wpscholar / pagination.php
Last active December 28, 2020 21:48
Create numeric pagination in WordPress
<?php
/**
* Create numeric pagination in WordPress
*/
// Get total number of pages
global $wp_query;
$total = $wp_query->max_num_pages;
// Only paginate if we have more than one page
@wpscholar
wpscholar / append-nav-items.php
Last active March 5, 2024 04:26
Add a custom link to the end of a menu that uses the wp_nav_menu() function
<?php
/**
* Add a custom link to the end of a specific menu that uses the wp_nav_menu() function
*/
add_filter('wp_nav_menu_items', 'add_admin_link', 10, 2);
function add_admin_link($items, $args){
if( $args->theme_location == 'footer_menu' ){
$items .= '<li><a title="Admin" href="'. esc_url( admin_url() ) .'">' . __( 'Admin' ) . '</a></li>';
}
@wpscholar
wpscholar / contextual-template-loading.php
Last active September 27, 2015 09:47
WordPress Contextual Template Loading
<?php
/**
* WordPress contextual template loading
*/
function mpress_load_template( $name ){
if( is_front_page() ){
if( is_home() ){
return locate_template( array(
$name.'-home.php',