Category Archives: WordPress

Set WordPress permissions recursively with CLI in Linux

To easily change the permissions, just navigate to the WordPress install folder, and execute this two commands:

#Set all directories recursively to 755:
find . -type d -exec chmod 0755 {} +

#Set all files recursively to 644:
find . -type f -exec chmod 0644 {} +

If you are not in the current directory you will need to add the desired one after find, something like:

find /top/path/here -type d -exec chown www-data:www-data {} \;

Redirect users/customers away from wp-admin

Use this code in the functions.php file for the active theme. Currently it will redirect users to /my-account – change as you like

add_action( 'init', 'blockusers_init' );
function blockusers_init() {
if ( is_admin() && ! current_user_can( 'administrator' ) &&
! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
wp_redirect( home_url() . "/my-account" );
exit;
}
}

Theme Options Page

Sample code for theme options page. It can be added to the functions.php file of the theme, or extend it and put in on different file and then include the file in functions.php
Note that the fields are not sanitized, refer to WordPress site for more info.


 
<div id="theme-options-wrap">
<div class="icon32" id="icon-tools"></div>
<h2>Theme Settings</h2>
Update various settings throughout your website.

<form action="options.php" enctype="multipart/form-data" method="post"><!--?php settings_fields('theme_options'); ?--> <!--?php do_settings_sections(__FILE__); ?-->
<p class="submit"><input class="button-primary" type="submit" name="Submit" value="<?php esc_attr_e('Save Changes'); ?>" /></p>

</form></div>
<!--?php <br ?-->
} add_action('admin_init', 'register_and_build_fields');

function register_and_build_fields() {

register_setting('theme_options', 'theme_options', 'validate_setting');

add_settings_section('homepage_settings', 'Homepage Settings', 'section_homepage', __FILE__);

add_settings_section('footer_settings', 'Footer Settings', 'section_footer', __FILE__);

function section_homepage() {

}

function section_footer() {

}

add_settings_field('button1text', 'Button 1 Text', 'button1text_setting', __FILE__, 'homepage_settings');

add_settings_field('button1link', 'Button 1 URL', 'button1link_setting', __FILE__, 'homepage_settings');

add_settings_field('button2text', 'Button 2 Text', 'button2text_setting', __FILE__, 'homepage_settings');

add_settings_field('button2link', 'Button 2 URL', 'button2link_setting', __FILE__, 'homepage_settings');

add_settings_field('button3text', 'Button 3 Text', 'button3text_setting', __FILE__, 'homepage_settings');

add_settings_field('button3link', 'Button 3 URL', 'button3link_setting', __FILE__, 'homepage_settings');

add_settings_field('phonenumber', 'Phone Number', 'phonenumber', __FILE__, 'footer_settings');

add_settings_field('facebookurl', 'Facebook URL', 'facebookurl', __FILE__, 'footer_settings');

add_settings_field('googleurl', 'Google+ URL', 'googleurl', __FILE__, 'footer_settings');

add_settings_field('twitterurl', 'Twitter URL', 'twitterurl', __FILE__, 'footer_settings');

}

function validate_setting($theme_options) {

return $theme_options;

}

function button1text_setting() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[button1text_setting]" value="{$options[" />";

}

function button1link_setting() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[button1link_setting]" value="{$options[" />";

}

function button2text_setting() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[button2text_setting]" value="{$options[" />";

}

function button2link_setting() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[button2link_setting]" value="{$options[" />";

}

function button3text_setting() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[button3text_setting]" value="{$options[" />";

}

function button3link_setting() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[button3link_setting]" value="{$options[" />";

}

function phonenumber() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[phonenumber]" value="{$options[" />";

}

function facebookurl() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[facebookurl]" value="{$options[" />";

}

function googleurl() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[googleurl]" value="{$options[" />";

}

function twitterurl() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[twitterurl]" value="{$options[" />";

}

add_action('admin_menu', 'theme_options_page');

function theme_options_page() {

add_menu_page('Theme Settings', 'Theme Settings', 'administrator', __FILE__, 'build_options_page', '', '62.2');

}

Then we create a variable with all our settings so we can use it in the template.

<?php $options = get_option('theme_options'); ?>

Then we can use some of the options like this:

<a href="<?php echo $options['button1link_setting']; ?>"><?php echo $options['button1text_setting']; ?></a>

Remove comments

This code snippet will remove the comments from everywhere, just put in the functions.php file in the theme. Here is the code:

<?php
// Removes from admin menu
add_action( 'admin_menu', 'my_remove_admin_menus' );
function my_remove_admin_menus() {
    remove_menu_page( 'edit-comments.php' );
}
// Removes from post and pages
add_action('init', 'remove_comment_support', 100);

function remove_comment_support() {
    remove_post_type_support( 'post', 'comments' );
    remove_post_type_support( 'page', 'comments' );
}
// Removes from admin bar
function mytheme_admin_bar_render() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu('comments');
}
add_action( 'wp_before_admin_bar_render', 'mytheme_admin_bar_render' );
?>

Blank Widget

This example widget code, place the code in separate file, then include it in finctions.php like that:

require( get_template_directory() . '/menu-widget.php' );

Register the widget(match the class names!)-

    function register_my_widget() {  
        register_widget( 'example_widget' );  
    }  

And call the widget:

add_action( 'widgets_init', function(){
     register_widget( 'My_Widget' );
});

And here it comes the widget code:

<?php
/**
 * Example Widget Class
 */
class example_widget extends WP_Widget {
 
 
    /** constructor -- name this the same as the class above */
    function example_widget() {
        parent::WP_Widget(false, $name = 'Example Text Widget');	
    }
 
    /** @see WP_Widget::widget -- do not rename this */
    function widget($args, $instance) {	
        extract( $args );
        $title 		= apply_filters('widget_title', $instance['title']);
        $message 	= $instance['message'];
        ?>
              <?php echo $before_widget; ?>
                  <?php if ( $title )
                        echo $before_title . $title . $after_title; ?>
							<ul>
								<li><?php echo $message; ?></li>
							</ul>
              <?php echo $after_widget; ?>
        <?php
    }
 
    /** @see WP_Widget::update -- do not rename this */
    function update($new_instance, $old_instance) {		
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['message'] = strip_tags($new_instance['message']);
        return $instance;
    }
 
    /** @see WP_Widget::form -- do not rename this */
    function form($instance) {	
 
        $title 		= esc_attr($instance['title']);
        $message	= esc_attr($instance['message']);
        ?>
         <p>
          <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> 
          <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
        </p>
		<p>
          <label for="<?php echo $this->get_field_id('message'); ?>"><?php _e('Simple Message'); ?></label> 
          <input class="widefat" id="<?php echo $this->get_field_id('message'); ?>" name="<?php echo $this->get_field_name('message'); ?>" type="text" value="<?php echo $message; ?>" />
        </p>
        <?php 
    }
 
 
} // end class example_widget
add_action('widgets_init', create_function('', 'return register_widget("example_widget");'));
?>


You can use ultimate posts widget to display posts – ultimate-posts-widget.1.8.1

Excerpt custom function – character length

This code will limit the excerpt to the desired number of character, without truncating the last word. The function –

function get_excerpt($count){
  $permalink = get_permalink($post->ID);
  $excerpt = get_the_content();
  $excerpt = strip_tags($excerpt);
  $excerpt = substr($excerpt, 0, $count);
  $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
  $excerpt = $excerpt.'... <a href="'.$permalink.'">more</a>';
  return $excerpt;
}

Echoing the excerpt

<?php echo get_excerpt(125); ?>

Display Posts From Single Category in a page

 

                                                                                                            <?php
                                                                                                               // The Query
                                                                                                               query_posts( array ( 'category_name' => 'uncategorized', 'posts_per_page' => 4 ) );
                                                                                                               ?>
                           
                                                                                            <?php while ( have_posts() ) : the_post();
                                                                                                                       echo '<h1>';
                                                                                                                       the_title();
                                                                                                                       echo '</h1>';
                                                                                                                       the_excerpt();
                                                                                                                       echo get_the_time( 'j.n.Y'); 
                                                                                                                       echo ' <p class="postinfo">Written by: ';
                                                                                                                       the_author_posts_link();
                                                                                                                       echo 'Posted on ';
                                                                                                                       the_date();
                                                                                                                       echo 'Categories: ';
                                                                                                                       the_category(', ');
                                                                                                                       echo '</p>';
                                                                                                                       endwhile; ?>

                                                                                                                         <?php

                                                                                                                         // Reset Query
                                                                                                                         wp_reset_query();

                                                                                                                         ?>

Display Last Post From Category

<?php $cat_name = uncategorized; //the certain category ID
$latest_cat_post = new WP_Query( array('posts_per_page' => 1, 'category__in' => array($cat_id)));
if( $latest_cat_post->have_posts() ) : while( $latest_cat_post->have_posts() ) : $latest_cat_post->the_post();  ?>

<h1><?php the_title(); ?></h1>	

<?php the_content(); ?>

<?php endwhile; endif; ?>

Adding Custom Menu To WordPress Theme

This snippet is for the functions.php file

add_theme_support( 'menus' );

or


register_nav_menu( 'primary', __( 'Primary Menu', 'twentytwelve' ));
register_nav_menu( 'footer', __( 'Footer Menu', 'twentytwelve' ));

This is where you want the menu to appear:

<?php wp_nav_menu( array( 'sort_column', 'menu_order', 'container_class' => 'menu-header' ) ); ?>

Twenty Eleven – remove “Theme options” tab

This code snippet remove tabs – “Theme Options”; “Header” and “Custom Background” from the twenty eleven theme

//Remove the custom options provided by the default twentyeleven theme.
add_action( ‘after_setup_theme’,’remove_twentyeleven_options’, 100 );
function remove_twentyeleven_options() {

	remove_custom_background();
	remove_custom_image_header();
	remove_action(‘admin_menu’, ‘twentyeleven_theme_options_add_page’);

}