Create Sitemap HTML Page

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php 
 
 /*
 Template Name: Sitemap Page
 */
 
get_header();
 
	/* PAGES
	----------------------------------------------*/
 
	// Get the navigation items to display the same order and save them in array to exclude later
 
	$items = wp_get_nav_menu_items( 'my menu id' );
	$exclude_pages = get_the_ID(); // to not display current sitemap page
 
	echo '<ul>';
 
 	foreach( $items as $item )  {
 
                $class = '';
		$exclude_pages .= ',' . $item->object_id;
 
		if( $item->menu_item_parent != 0) {
			$class = ' class="sitemap-item-child"';
		}
 
		?>
			<li<?php echo $class ?>>
				<a href=""><?php echo $item->title; ?></a>
			</li>
 
		<?php
 
	}
 
	// List the rest of the pages that are not in the navigation
	wp_list_pages(
			 array(
			   'exclude' => $exclude_pages,
			   'title_li' => '',
			 )
		);
 
	echo '</ul>';
 
	/* CUSTOM POST TYPES 
	----------------------------------------------*/
 
	foreach( get_post_types( array('public' => true) ) as $post_type ) {
 
	  if ( in_array( $post_type, array('post','page','attachment') ) ) continue;
 
	  $item = get_post_type_object( $post_type );
 
	  echo '<h2>'.$item->labels->name.'</h2>';
 
	  $args = array(
		'post_type' => $post_type,
		'posts_per_page'    => -1
	  );
 
	  $query = new WP_Query( $args );
 
	  echo '<ul>';
 
		while ( $query->have_posts() ) {
 
			$query->the_post();
			echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
 
		}
 
	  echo '</ul>';
 
	}
 
 
get_footer();

How to change Genesis breadcrumbs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 
add_filter('genesis_breadcrumb_args', 'custom_breadcrumbs_labels');
 
function custom_breadcrumbs_labels( $args ) {
  $args['labels']['prefix'] = '';
  return $args;
}
 
/*
Default arguments:
 
$this->args = array(
		'home'                    => __( 'Home', 'genesis' ),
		'sep'                     => 
                'list_sep'                => ', ',
		'prefix'                  => '<div class="breadcrumb">',
		'suffix'                  => '</div>',
		'heirarchial_attachments' => true,
		'heirarchial_categories'  => true,
		'labels' => array(
				'prefix'    => __( 'You are here: ', 'genesis' ),
				'author'    => __( 'Archives for ', 'genesis' ),
				'category'  => __( 'Archives for ', 'genesis' ),
				'tag'       => __( 'Archives for ', 'genesis' ),
				'date'      => __( 'Archives for ', 'genesis' ),
				'search'    => __( 'Search for ', 'genesis' ),
				'tax'       => __( 'Archives for ', 'genesis' ),
				'post_type' => __( 'Archives for ', 'genesis' ),
				'404'       => __( 'Not found: ', 'genesis' ) 
                            )
                  );
 
*/

Rename table prefix on existing WordPress site

Follow these few steps to rename the table prefix of your existing WordPress site.

Edit wp-config.php and change

$table_prefix = 'wp_';

to any other prefix, try to make it abstract and hard to be guessed by anyone, for example:

$table_prefix = 'v5r33w67ee_';

Rename the tables in your database, these are the SQL queries to rename the core tables:

RENAME table `wp_users` TO `v5r33w67ee_users`;
RENAME table `wp_usermeta` TO `v5r33w67ee_usermeta`;
RENAME table `wp_comments` TO `v5r33w67ee_comments`;
RENAME table `wp_commentmeta` TO `v5r33w67ee_commentmeta`;
RENAME table `wp_links` TO `v5r33w67ee_links`;
RENAME table `wp_options` TO `v5r33w67ee_options`;
RENAME table `wp_posts` TO `v5r33w67ee_posts`;
RENAME table `wp_postmeta` TO `v5r33w67ee_postmeta`;
RENAME table `wp_terms` TO `v5r33w67ee_terms`;
RENAME table `wp_term_taxonomy` TO `v5r33w67ee_term_taxonomy`;
RENAME table `wp_term_relationships` TO `v5r33w67ee_term_relationships`;

Add the queries to rename any other existing table that have been created from other plugins or themes.
You can also rename the tables in phpMyAdmin, by clicking on the operations tab

Update the references to the old table prefix in the usermeta and options table running these SQL queries.

UPDATE `v5r33w67ee_usermeta` SET `meta_key` = REPLACE( `meta_key`, 'wp_', 'v5r33w67ee_' );
UPDATE `v5r33w67ee_options` SET `option_name` = 'v5r33w67ee_user_roles' WHERE `option_name` = 'wp_user_roles';

Example to fix navigation on scroll

This is an example to fix the navigation to the top of the page when scrolling down. This case scenario is a header (site-header) and a navigation under it (nav-primary).
It takes in consideration if the user is logged in to calculate the WordPress adminbar height.

functions.php

1
2
3
4
5
6
7
8
9
add_action( 'wp_enqueue_scripts', 'load_my_scripts' );
function load_my_scripts() {
 
     wp_enqueue_script( 'my-script', get_bloginfo( 'stylesheet_directory' ) . '/assets/js/my-scrip.js', array( 'jquery') );
 
     wp_localize_script( 'my-script', 'my_vars', array(
			'logged_in' => is_user_logged_in() ? 'yes' : 'no',
     ));
}

my-script.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
jQuery(function( $ ){
 
	var navigation = $('.nav-primary');	
	var header     = $('.site-header');
 
	var navigation_height = parseInt( navigation.css('height') );
	var header_height     = parseInt( header.css('height') );
	var adminbar_height   = 0;
 
	if( my_vars.logged_in == 'yes' ){
		adminbar_height = 	parseInt($('#wpadminbar').css('height'));
	}
 
	$(window).scroll( function(){
 
	    var scrollTop              = $(this).scrollTop();
	    var topDistance_navigation = navigation.offset().top;
       	    var topDistance_header     = header.offset().top;
 
	    // if navigation hits the top stay fixed
	    if ( topDistance_navigation - adminbar_height < scrollTop ) {
 
                 if( ! navigation.hasClass('nav-fixed') ) {
 
            	     navigation.addClass('nav-fixed');
		     navigation.css('top', adminbar_height);
 
            	}
 
            } 
 
            // if header hits the top remove fixed class
           	if( header_height - adminbar_height  > scrollTop ) {
            	navigation.removeClass('nav-fixed');
            }
        });
 
});

style.css

1
2
3
4
5
6
.nav-primary.nav-fixed{
   position:fixed;
   left:0px; 
   width:100%;
   z-index: 500;
}

Change favicon on Genesis

1
2
3
4
5
6
7
8
// Add this function to your childtheme functions file
function my_custom_favicon( $favicon_url ) {
 
        $path_to_my_icon = 'the path to the icon';
	return $path_to_my_icon;
 }
 
 add_filter( 'genesis_pre_load_favicon', 'my_custom_favicon' );

Define basic plugin constants

1
2
3
4
if( !defined('MY_PLUGIN_DIR') )      { define('MY_PLUGIN_DIR', plugin_dir_path( __FILE__ )); }
if( !defined('MY_PLUGIN_URL') )      { define('MY_PLUGIN_URL', plugin_dir_url( __FILE__ )); }
if( !defined('MY_PLUGIN_FILE') )     { define('MY_PLUGIN_FILE',  __FILE__ ); }
if( !defined('MY_PLUGIN_VERSION') )  { define('MY_PLUGIN_VERSION', 'X.X'); }

Add a shortcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function my_shortcode_function( $atts ) {
 
      $atts = shortcode_atts(
	   array(
	     'message' => 'My default message',
	     'class'   => 'default'
	   ), 
	   $atts);
 
      $result  = '<div class="' . esc_attr( $atts['class'] )  . '">' . $atts['message'] . '</div>'; 
      return $result; 
} 
 
add_shortcode('myshortcode', 'my_shortcode_function'); 
 
/************ 
 
usage :
[myshortcode message="This is my shortcode text" class="test"] 
 
If the shortcode produces a lot of HTML use ob_start to capture output and convert to string.
 
ob_start(); 
......... 
return ob_get_clean(); 
 
NOTE: shortodes always require returned values, not echo of values.
*/

http://codex.wordpress.org/Shortcode_API
http://codex.wordpress.org/Function_Reference/add_shortcode

Add submenu item to admin menu

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function);
example for tools menu
*/
add_action('admin_menu', 'add_submenu_item');
 
function add_submenu_item(){
     add_submenu_page( 'tools.php', 'My Tool', 'My Tool', 'manage_options', 'my-tool', 'display_page');
     // or
     // add_management_page( 'My Tool', 'My Tool', 'manage_options', 'my-tool', 'display_page' );
} 
 
/**
- Settings: options-general.php or use add_options_page()
- Dashboard: index.php or use add_dashboard_page()
- Posts: edit.php or use add_posts_page()
- Pages: edit.php?post_type=page or use add_pages_page()
- Custom post type: edit.php?post_type=custom_post_type
- Media: upload.php  or use add_media_page()
- Comments: edit-comments.php or use add_comments_page()
- Appearance: themes.php or use add_theme_page()
- Plugins: plugins.php or use add_plugins_page()
- Users: users.php or use add_users_page()
- Tools: tools.php or use add_management_page()
*/

Woocommerce, direct to checkout page on one click

You can directly go to the checkout page skipping the product page and the cart page by adding a link to a button and the following code on your functions file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
This is the structure of the link you must put on your button:
http://my-site.com?add-to-cart=idProduct
For example:
http://my-site.com?add-to-cart=28
*/
 
// Code tu add on your functions.php file
add_filter ('add_to_cart_redirect', 'redirect_to_checkout_page');
 
function edirect_to_checkout_page() {
 
	global $woocommerce;
 
	$checkout_url = $woocommerce->cart->get_checkout_url();
	return $checkout_url;
}