├── README.md
├── baseplugin.php
├── bootstrap_navwalker_images.php
├── custom-post-type.php
├── functions.php
├── gallery-html-custom.php
├── gallery-loop.php
├── image-background.php
├── loop-custom.php
├── loop-pagination.php
├── loop-template-filter.php
├── loop.php
├── shortcode.php
├── taxonomy-menu.php
├── taxonomy.php
└── woocommerce-optimize-enque-scripts-styles.php
/README.md:
--------------------------------------------------------------------------------
1 | # WordPress Snippets Snippets
2 |
3 | A collection of WordPress utilities by [MarchettiDesign.net](http://www.marchettidesign.net/)
4 |
5 |
6 | ## List of the utilities
7 |
8 | - Custom Post Type
9 |
10 | - Basic Functions.php
11 |
12 | - Gallery Loop
13 |
14 | - Image Background Single Post / Page
15 |
16 | - Loop Custom
17 |
18 | - Loop with Pagination
19 |
20 | - Loop with filter based on Template selected
21 |
22 | - Loop Standard
23 |
24 | - Taxonomy Menu
25 |
26 | - Taxonomy
27 |
28 | - Woocommerce Scripts Optimizations
29 |
30 |
31 | ## Usage
32 | Copy and paste the snippets.
33 |
34 | ## Licensing
35 | MIT © 2016 [Andrea Marchetti](http://www.marchettidesign.net/)
36 |
--------------------------------------------------------------------------------
/baseplugin.php:
--------------------------------------------------------------------------------
1 | '. __( 'Contact us now!', 'bsp' ) .'
';
63 | }
64 | add_action( 'wp_body_open', 'bsp_header_code' );
65 |
66 |
67 | /*
68 |
69 | HOOKS
70 |
71 | Modo per aggiungere o modificare codice all'interno di wordpress in punto definito
72 | es. hooks per aggiungere codice dopo il body, nel footer, hooks per comabiare il titolo delle pagine wordpress
73 |
74 | 2 tipi di hooks
75 |
76 | - actions inseriscono codice in un punto preciso di wordpress https://developer.wordpress.org/plugins/hooks/actions/
77 | - filtri filtrano dati per esempio (titolo o il content) facendo qualcosa https://developer.wordpress.org/plugins/hooks/filters/
78 |
79 | */
80 |
81 | /* filter */
82 | function bsp_change_and_link_excerpt( $more ) {
83 |
84 | return $more . ' \n";
25 | }
26 |
27 | /**
28 | * @see Walker::start_el()
29 | * @since 3.0.0
30 | *
31 | * @param string $output Passed by reference. Used to append additional content.
32 | * @param object $item Menu item data object.
33 | * @param int $depth Depth of menu item. Used for padding.
34 | * @param int $current_page Menu item ID.
35 | * @param object $args
36 | */
37 | public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
38 | $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
39 |
40 | /**
41 | * Dividers, Headers or Disabled
42 | * =============================
43 | * Determine whether the item is a Divider, Header, Disabled or regular
44 | * menu item. To prevent errors we use the strcasecmp() function to so a
45 | * comparison that is not case sensitive. The strcasecmp() function returns
46 | * a 0 if the strings are equal.
47 | */
48 | if ( strcasecmp( $item->attr_title, 'divider' ) == 0 && $depth === 1 ) {
49 | $output .= $indent . '
';
50 | } else if ( strcasecmp( $item->title, 'divider') == 0 && $depth === 1 ) {
51 | $output .= $indent . '';
52 | } else if ( strcasecmp( $item->attr_title, 'dropdown-header') == 0 && $depth === 1 ) {
53 | $output .= $indent . '' . esc_attr( $item->title ) . '';
56 | } else {
57 |
58 | $class_names = $value = '';
59 |
60 | $classes = empty( $item->classes ) ? array() : (array) $item->classes;
61 | $classes[] = 'menu-item-' . $item->ID;
62 |
63 | $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
64 |
65 | if ( $args->has_children )
66 | $class_names .= ' dropdown yamm-fullwidth';
67 |
68 | if ( in_array( 'current-menu-item', $classes ) )
69 | $class_names .= ' active';
70 |
71 | $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
72 |
73 | $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
74 | $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
75 |
76 | $output .= $indent . '';
77 |
78 | $atts = array();
79 | $atts['title'] = ! empty( $item->title ) ? $item->title : '';
80 | $atts['target'] = ! empty( $item->target ) ? $item->target : '';
81 | $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
82 |
83 | // If item has_children add atts to a.
84 | if ( $args->has_children && $depth === 0 ) {
85 | $atts['href'] = '#';
86 | $atts['data-toggle'] = 'dropdown';
87 | $atts['class'] = 'dropdown-toggle';
88 | $atts['aria-haspopup'] = 'true';
89 | $atts['aria-expanded'] = 'false';
90 | } else {
91 | $atts['href'] = ! empty( $item->url ) ? $item->url : '';
92 | }
93 |
94 | $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
95 |
96 | $attributes = '';
97 | foreach ( $atts as $attr => $value ) {
98 | if ( ! empty( $value ) ) {
99 | $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
100 | $attributes .= ' ' . $attr . '="' . $value . '"';
101 | }
102 | }
103 |
104 | $item_output = $args->before;
105 |
106 | /*
107 | * Glyphicons
108 | * ===========
109 | * Since the the menu item is NOT a Divider or Header we check the see
110 | * if there is a value in the attr_title property. If the attr_title
111 | * property is NOT null we apply it as the class name for the glyphicon.
112 | */
113 | if ( ! empty( $item->attr_title ) )
114 | $item_output .= ' ';
115 | else
116 | $item_output .= '';
117 |
118 | $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
119 | $item_output .= ( $args->has_children && 0 === $depth ) ? ' ' : '';
120 | $item_output .= $args->after;
121 |
122 | $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
123 | }
124 | }
125 |
126 | /**
127 | * Traverse elements to create list from elements.
128 | *
129 | * Display one element if the element doesn't have any children otherwise,
130 | * display the element and its children. Will only traverse up to the max
131 | * depth and no ignore elements under that depth.
132 | *
133 | * This method shouldn't be called directly, use the walk() method instead.
134 | *
135 | * @see Walker::start_el()
136 | * @since 2.5.0
137 | *
138 | * @param object $element Data object
139 | * @param array $children_elements List of elements to continue traversing.
140 | * @param int $max_depth Max depth to traverse.
141 | * @param int $depth Depth of current element.
142 | * @param array $args
143 | * @param string $output Passed by reference. Used to append additional content.
144 | * @return null Null on failure with no changes to parameters.
145 | */
146 | public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
147 | if ( ! $element )
148 | return;
149 |
150 | $id_field = $this->db_fields['id'];
151 |
152 | // Display this element.
153 | if ( is_object( $args[0] ) )
154 | $args[0]->has_children = ! empty( $children_elements[ $element->$id_field ] );
155 |
156 | parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
157 | }
158 |
159 | /**
160 | * Menu Fallback
161 | * =============
162 | * If this function is assigned to the wp_nav_menu's fallback_cb variable
163 | * and a manu has not been assigned to the theme location in the WordPress
164 | * menu manager the function with display nothing to a non-logged in user,
165 | * and will add a link to the WordPress menu manager if logged in as an admin.
166 | *
167 | * @param array $args passed from the wp_nav_menu function.
168 | *
169 | */
170 | public static function fallback( $args ) {
171 | if ( current_user_can( 'manage_options' ) ) {
172 |
173 | extract( $args );
174 |
175 | $fb_output = null;
176 |
177 | if ( $container ) {
178 | $fb_output = '<' . $container;
179 |
180 | if ( $container_id )
181 | $fb_output .= ' id="' . $container_id . '"';
182 |
183 | if ( $container_class )
184 | $fb_output .= ' class="' . $container_class . '"';
185 |
186 | $fb_output .= '>';
187 | }
188 |
189 | $fb_output .= '';
199 | $fb_output .= '';
200 |
201 | if ( $container )
202 | $fb_output .= '' . $container . '>';
203 |
204 | echo $fb_output;
205 | }
206 | }
207 | }
208 |
209 | add_filter('wp_nav_menu_objects', 'ad_filter_menu', 10, 2);
210 |
211 | function ad_filter_menu($sorted_menu_objects, $args) {
212 |
213 | // check for the right menu to filter
214 | // here we check for the menu with name 'Posts Menu'
215 | // given that you call it in you theme with:
216 | // wp_nav_menu( array( 'menu' => 'Posts Menu' ) );
217 | // if you call the menu using theme_location, eg:
218 | // wp_nav_menu( array( 'theme_location' => 'top_manu' ) );
219 | // check for:
220 | if ($args->theme_location != 'header')
221 | //if ($args->menu != 'Main Menu')
222 | return $sorted_menu_objects;
223 |
224 | // edit the menu objects
225 | foreach ($sorted_menu_objects as $menu_object) {
226 | // searching for menu items linking to posts or pages
227 | // can add as many post types to the array
228 | if ( in_array($menu_object->object, array('page')) ) {
229 |
230 | // set the title to the post_thumbnail if available
231 | // thumbnail size is the second parameter of get_the_post_thumbnail()
232 |
233 | if($menu_object->menu_item_parent != 0 ){ // if the item have a menu parent
234 | // $menu_object->title = has_post_thumbnail($menu_object->object_id) ? get_the_post_thumbnail($menu_object->object_id, 'thumbnail') . $menu_object->title : $menu_object->title;
235 |
236 | }
237 |
238 | }
239 | }
240 |
241 | return $sorted_menu_objects;
242 | }
243 |
--------------------------------------------------------------------------------
/custom-post-type.php:
--------------------------------------------------------------------------------
1 | __('Services' , 'language-slug'),
15 | 'singular_name' => __('Service', 'language-slug'),
16 | 'add_new' => __('Add Service', 'language-slug'),
17 | 'add_new_item' => __('Add New Service', 'language-slug'),
18 | 'edit_item' => __('Edit Service', 'language-slug'),
19 | 'new_item' => __('New Service', 'language-slug'),
20 | 'all_items' => __('All Services', 'language-slug'),
21 | 'view_item' => __('View Service', 'language-slug'),
22 | 'search_items' => __('Search Service' , 'language-slug'),
23 | 'not_found' => __('Service Not found', 'language-slug'),
24 | 'not_found_in_trash' => __('Service Not found in the trash', 'language-slug'),
25 | );
26 |
27 | $args = array(
28 | 'labels' => $labels,
29 | 'public' => true,
30 | 'rewrite' => array('slug' => 'services'),
31 | 'has_archive' => true,
32 | 'hierarchical' => true,
33 | 'menu_position' => 22,
34 | 'menu_icon' => 'dashicons-welcome-write-blog',
35 | 'supports' => array(
36 | 'title',
37 | 'editor',
38 | 'thumbnail',
39 | 'excerpt',
40 | 'page-attributes'
41 | ),
42 |
43 | );
44 |
45 | register_post_type('my_service_cpt', $args);
46 | }
47 |
48 | ?>
49 |
--------------------------------------------------------------------------------
/functions.php:
--------------------------------------------------------------------------------
1 | esc_html__( 'Header', 'slug_theme' )
37 | ) );
38 |
39 | // Load theme languages
40 | load_theme_textdomain( 'slug_theme', get_template_directory().'/languages' );
41 |
42 | }
43 |
44 | }
45 | add_action( 'after_setup_theme', 'slug_theme_setup' );
46 |
47 |
48 | /* Register sidebars
49 | /* ------------------------------------ */
50 | if ( ! function_exists( 'slug_theme_sidebars' ) ) {
51 |
52 |
53 | function slug_theme_sidebars() {
54 | register_sidebar(array( 'name' => esc_html__( 'Primary', 'slug_theme' ),'id' => 'primary','description' => esc_html__( 'Normal full width sidebar.', 'slug_theme' ), 'before_widget' => '
','after_widget' => '
','before_title' => '
','after_title' => '
'));
55 | }
56 |
57 | }
58 | add_action( 'widgets_init', 'slug_theme_sidebars' );
59 |
60 |
61 | /* Enqueue javascript
62 | /* ------------------------------------ */
63 | if ( ! function_exists( 'slug_theme_scripts' ) ) {
64 |
65 | function slug_theme_scripts() {
66 |
67 | // all script
68 | wp_enqueue_script( 'slug_theme-script', get_template_directory_uri() . '/js/script.js', array( 'jquery' ),'', true );
69 | if ( is_singular() && get_option( 'thread_comments' ) ) { wp_enqueue_script( 'comment-reply' ); }
70 |
71 | }
72 |
73 | }
74 | add_action( 'wp_enqueue_scripts', 'slug_theme_scripts' );
75 |
76 |
77 | /* Enqueue css
78 | /* ------------------------------------ */
79 | if ( ! function_exists( 'slug_theme_styles' ) ) {
80 |
81 | function slug_theme_styles() {
82 | wp_enqueue_style( 'slug_theme-style', get_template_directory_uri().'/style.css');
83 |
84 | }
85 |
86 | }
87 | add_action( 'wp_enqueue_scripts', 'slug_theme_styles' );
88 |
89 | ?>
90 |
--------------------------------------------------------------------------------
/gallery-html-custom.php:
--------------------------------------------------------------------------------
1 | ID;
14 | $gallery = "";
15 |
16 | if (empty($pid)) {$pid = $post['ID'];}
17 |
18 | if (!empty( $atts['ids'] ) ) {
19 | $atts['orderby'] = 'post__in';
20 | $atts['include'] = $atts['ids'];
21 | }
22 |
23 | extract(shortcode_atts(array('orderby' => 'menu_order ASC, ID ASC', 'include' => '', 'id' => $pid, 'itemtag' => 'dl', 'icontag' => 'dt', 'captiontag' => 'dd', 'columns' => 3, 'size' => 'large', 'link' => 'file'), $atts));
24 |
25 | $args = array('post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'orderby' => $orderby);
26 |
27 | if (!empty($include)) {$args['include'] = $include;}
28 | else {
29 | $args['post_parent'] = $id;
30 | $args['numberposts'] = -1;
31 | }
32 |
33 | if ($args['include'] == "") { $args['orderby'] = 'date'; $args['order'] = 'asc';}
34 |
35 | $images = get_posts($args);
36 |
37 | $gallery .= '
';
38 |
39 |
40 |
41 | foreach ( $images as $image ) {
42 |
43 | $thumbnail = wp_get_attachment_image_src($image->ID, 'thumbnail');
44 |
45 | $thumbnail = $thumbnail[0];
46 |
47 | $gallery .= "

";
48 | }
49 |
50 | $gallery .= '
';
51 |
52 | return $gallery;
53 | } ?>
54 |
--------------------------------------------------------------------------------
/gallery-loop.php:
--------------------------------------------------------------------------------
1 | ID, false );
42 |
43 | if($gallery != ''){
44 |
45 | $ids = explode( ",", $gallery['ids'] );
46 |
47 | /* loop image gallery */
48 |
49 | foreach( $ids as $id ) {
50 |
51 | $image_url = wp_get_attachment_image_src( $id , 'medium' );
52 | $thumbnail_image = get_posts(array('p' => $id, 'post_type' => 'attachment'));
53 |
54 | ?>
55 |
56 |
57 |
58 |

59 |
60 |
post_excerpt; ?>
61 |
62 |
63 |
64 |
69 |
70 |
71 |
72 |
--------------------------------------------------------------------------------
/image-background.php:
--------------------------------------------------------------------------------
1 | ID ), 'medium' );
8 |
9 | ?>
10 |
11 |
12 |
--------------------------------------------------------------------------------
/loop-custom.php:
--------------------------------------------------------------------------------
1 |
9 | 'page',
15 | 'posts_per_page'=> 3,
16 | 'orderby' => 'menu_order',
17 | 'order' => 'ASC',
18 | ));
19 |
20 | /* Wp Query - page + taxonomy + menu order */
21 |
22 | // $custom_post = new WP_Query( array(
23 | // 'post_type' => 'page',
24 | // 'posts_per_page'=> 3,
25 | // 'orderby' => 'menu_order',
26 | // 'order' => 'ASC',
27 | // 'tax_query' => array(
28 | // array(
29 | // 'taxonomy' => 'home_positions',
30 | // 'field' => 'slug',
31 | // 'terms' => 'top'
32 | // )
33 | // )
34 | // ));
35 |
36 | /* Wp Query - post + taxonomy */
37 |
38 | // $custom_post = new WP_Query( array(
39 | // 'post_type' => 'post',
40 | // 'tax_query' => array(
41 | // array(
42 | // 'taxonomy' => 'people',
43 | // 'field' => 'slug',
44 | // 'terms' => 'bob',
45 | // )
46 | // )
47 | // ));
48 |
49 | /* Wp Query - post + category */
50 |
51 | // $custom_post = new WP_Query( array(
52 | // 'post_type' => 'post',
53 | // 'category_name' => 'news',
54 | // 'posts_per_page'=> 3,
55 | // ));
56 |
57 |
58 | /* Wp Query - sub pages + menu order */
59 |
60 | // $this_page_id = $wp_query->post->ID;
61 | // $custom_post = new WP_Query( array(
62 | // 'post_type' => 'page',
63 | // 'post_parent' => $this_page_id,
64 | // 'posts_per_page' => 100,
65 | // 'orderby' => 'menu_order',
66 | // 'order' => 'ASC',
67 | // ));
68 |
69 |
70 |
71 | ?>
72 |
73 | have_posts()) : while($custom_post->have_posts()) : $custom_post->the_post(); ?>
74 |
75 |
76 | '','alt' => get_the_title())); ?>
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
--------------------------------------------------------------------------------
/loop-pagination.php:
--------------------------------------------------------------------------------
1 | 'page',
13 | 'posts_per_page' => 10,
14 | );
15 |
16 | // Get current page and append to custom query parameters array
17 | $custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
18 |
19 | // Instantiate custom query
20 | $custom_query = new WP_Query( $custom_query_args );
21 |
22 | // Pagination fix
23 | $temp_query = $wp_query;
24 | $wp_query = NULL;
25 | $wp_query = $custom_query;
26 |
27 | // Output custom query loop
28 | if ( $custom_query->have_posts() ) :
29 | while ( $custom_query->have_posts() ) :
30 | $custom_query->the_post();
31 | // Loop
32 | ?>
33 |
34 |
35 | '','alt' => get_the_title())); ?>
36 |
37 |
38 |
39 |
40 |
41 |
42 | ';
51 |
52 | global $wp_query;
53 |
54 | $big = 999999999; // Need an unlikely integer
55 |
56 | echo paginate_links( array(
57 | 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
58 | 'format' => '?paged=%#%',
59 | 'current' => max( 1, get_query_var('paged') ),
60 | 'total' => $wp_query->max_num_pages
61 | ) );
62 |
63 | echo '
';
64 |
65 | // Reset main query object
66 | $wp_query = NULL;
67 | $wp_query = $temp_query;
68 |
69 | ?>
70 |
--------------------------------------------------------------------------------
/loop-template-filter.php:
--------------------------------------------------------------------------------
1 |
10 | 'page',
14 | 'no_found_rows' => true,
15 | 'post_status' => 'publish',
16 | 'orderby' => 'menu_order',
17 | 'order' => 'ASC',
18 | 'posts_per_page' => 3,
19 | 'meta_query' => array(
20 | array(
21 | 'key' => '_wp_page_template',
22 | 'value' => 'my-custom-template.php',
23 | )
24 | )
25 | ) ); ?>
26 |
27 | have_posts()) : while($template_pages->have_posts()) : $template_pages->the_post(); ?>
28 |
29 |
30 | '','alt' => get_the_title())); ?>
31 |
32 |