-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordpress.html
More file actions
1337 lines (1196 loc) · 60.4 KB
/
wordpress.html
File metadata and controls
1337 lines (1196 loc) · 60.4 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="./assets/images/fav.png">
<!-- meta tags -->
<title>Wordpress Code Snippets</title>
<meta name="description" content="Find the most useful WordPress code snippets to help you build your website. Get easy-to-implement code solutions for common WordPress tasks, including customizing themes, adding functionality, and optimizing performance. Save time and improve your WordPress development with our collection of code snippets.">
<!-- Bootstrap CSS -->
<link href="./assets/bootstrap/bootstrap.min.css" rel="stylesheet">
<link href="./assets/style.css" rel="stylesheet">
<link href="./assets/prism/prism.css" rel="stylesheet">
<link href="./assets/line-number/line-number.css" rel="stylesheet">
<script src="./assets/prism/prism.js"></script>
<script src="./assets/line-number/line-number.js"></script>
</head>
<body style="background-color:#fff">
<section class="main">
<header class="container-fluid darkHdr">
<div class="container">
<div class="row">
<div class="col-12 col-sm-6 d-flex align-items-center">
<div class="inner d-flex justify-content-center justify-content-sm-start">
<h1 class="logoText">
<a href="./">
<span><</span>Code Snippets<span>/></span>
</a>
</h1>
</div>
</div>
<div class="col-6 d-none d-sm-flex">
<div class="inner d-flex justify-content-end social">
<ul>
<li>
<a href="./wordpress.html">
<img src="./assets/images/icon-05.png">
</a>
</li>
<li>
<a href="./shopify.html">
<img src="./assets/images/icon-04.png">
</a>
</li>
<li>
<a href="https://github.com/jayantisolanki" target="_blank">
<img src="./assets/images/github.png">
</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</header>
<div class="ap_wrapper">
<div class="ap_newDesign left" id="sideBar">
<div class="inner">
<ul>
<li class="general">
<img src="./assets/images/book.png" alt="">Getting Started
</li>
<li data-topic="Basic Functions">
<a href="javascript:void(0)" onclick="scrollToSection('aa')">
Basic Functions
</a>
</li>
<li data-topic="Get Logo on Frontend">
<a href="javascript:void(0)" onclick="scrollToSection('ab')">
Get Logo on Frontend
</a>
</li>
<li data-topic="Get Page Featured Image">
<a href="javascript:void(0)" onclick="scrollToSection('ac')">
Get Page Featured Image
</a>
</li>
<li data-topic="Get Blog Lisitng Featured Image">
<a href="javascript:void(0)" onclick="scrollToSection('ad')">
Get Blog Lisitng Featured Image
</a>
</li>
<li data-topic="Get Categories on Single Post">
<a href="javascript:void(0)" onclick="scrollToSection('ae')">
Get Categories on Single Post
</a>
</li>
<li data-topic="Get Tags on Single Post">
<a href="javascript:void(0)" onclick="scrollToSection('af')">
Get Tags on Single Post
</a>
</li>
<li data-topic="Menu Functions">
<a href="javascript:void(0)" onclick="scrollToSection('ag')">
Menu Functions
</a>
<ul class="innerItems">
<li>
<a href="javascript:void(0)" onclick="scrollToSection('ag1')">
Add Custom Class to 'li' in Menu
</a>
</li>
<li>
<a href="javascript:void(0)" onclick="scrollToSection('ag2')">
Add Custom Class to 'menu anchor' in Menu
</a>
</li>
<li>
<a href="javascript:void(0)" onclick="scrollToSection('ag3')">
Override 'Submenu' class in Menu
</a>
</li>
<li>
<a href="javascript:void(0)" onclick="scrollToSection('ag4')">
Add 'Caret for Submenu' in Menu
</a>
</li>
</ul>
</li>
<li data-topic="Register Sidebar">
<a href="javascript:void(0)" onclick="scrollToSection('ah')">
Register Sidebar
</a>
</li>
<li data-topic="Create Shortcode">
<a href="javascript:void(0)" onclick="scrollToSection('ai')">
Create Shortcode
</a>
</li>
<li data-topic="Get Categories & Sub Categories of Posts">
<a href="javascript:void(0)" onclick="scrollToSection('aj')">
Get Categories & Sub Categories of Posts
</a>
</li>
<li data-topic="Wocommerce">
<a href="javascript:void(0)" onclick="scrollToSection('ak')">
Wocommerce
</a>
<ul class="innerItems">
<li>
<a href="javascript:void(0)" onclick="scrollToSection('ak1')">
Redirect after Login & Registration
</a>
</li>
<li>
<a href="javascript:void(0)" onclick="scrollToSection('ak2')">
Custom Woocommerce Login
</a>
</li>
<li>
<a href="javascript:void(0)" onclick="scrollToSection('ak3')">
Custom Woocommerce Login & registration Form
</a>
</li>
<li>
<a href="javascript:void(0)" onclick="scrollToSection('ak4')">
Logout From Woocommerce
</a>
</li>
<li>
<a href="javascript:void(0)" onclick="scrollToSection('ak5')">
Remove links of single products from products listing
</a>
</li>
<li>
<a href="javascript:void(0)" onclick="scrollToSection('ak6')">
Remove/Reduce Password Security
</a>
</li>
</ul>
</li>
<li data-topic="Get file size">
<a href="javascript:void(0)" onclick="scrollToSection('al')">
Get file size
</a>
</li>
<li data-topic="Upload Files from Fronend in wordpress using Ajax">
<a href="javascript:void(0)" onclick="scrollToSection('am')">
Upload Files from Fronend in wordpress using Ajax
</a>
</li>
<li data-topic="Register Custom Post">
<a href="javascript:void(0)" onclick="scrollToSection('an')">
Get Custom Post using wp-query
</a>
</li>
<li data-topic="Get Custom Post using wp-query">
<a href="javascript:void(0)" onclick="scrollToSection('ao')">
Get Custom Post using wp-query
</a>
</li>
<li data-topic="Check is Valid Business Emails">
<a href="javascript:void(0)" onclick="scrollToSection('ap')">
Check is Valid Business Emails
</a>
</li>
</ul>
</div>
</div>
<div class="ap_right_box">
<div class="inner_box">
<div class="right">
<div class="top_hdng">
<h3 class="hdng">
Get started with Code Snippets
</h3>
<p>
Code Snippets is a powerful, feature-packed frontend toolkit. Build anything—from prototype to production—in minutes.
</p>
</div>
<ul>
<!--- getting content on front end --->
<li id="aa">
<div>
<h3>
<span class="text">
# Basic Functions
</span>
</h3>
<p class="topicDesc">
We've listed some basic functions that can help you fetch content and perform other tasks in WordPress. Use these functions to streamline your website development and improve your overall WordPress experience.
</p>
<span class="function">
<pre class="line-numbers">
<code class="language-php">//Get site URL
get_site_url();
//Get site Title
the_title();
//Get post Permalink
the_permalink();
//Get Post URL
the_post_thumbnail_url();
//Get Post Date
the_date('d M Y');
//Get Author
the_author();
//Get Trim Content
wp_trim_words( get_the_content(), 30, '[...]' );
//Add hyphen to a string
sanitize_title_with_dashes(get_the_title());
//Get Excerpt
the_excerpt();
//Add theme support of Custom Logo
add_theme_support( 'custom-logo', $defaults );
//Add theme support of title tag
add_theme_support( 'title-tag' );
// Remove <p> and <br/> from Contact Form 7 (Contact form 7 automatically added paragraph and break tags)
add_filter('wpcf7_autop_or_not', '__return_false');
</code>
</pre>
</span>
</div>
</li>
<li id="ab">
<div>
<h3>
<span class="text">
# Get Logo on Frontend
</span>
</h3>
<p class="topicDesc">
With the following code, you can easily retrieve your logo on the frontend of your website. Improve your website's branding and user experience by displaying your logo prominently on your website.
</p>
<span class="function">
<pre class="line-numbers">
<code class="language-php">$custom_logo_id = get_theme_mod( 'custom_logo' );
$custom_logo_url = wp_get_attachment_image_url( $custom_logo_id , 'full' );
echo '<img src="' . esc_url( $custom_logo_url ) . '" alt="Site Logo">';</code>
</pre>
</span>
</div>
</li>
<li id="ac">
<div>
<h3>
<span class="text">
# Get Page Featured Image
</span>
</h3>
<p class="topicDesc">
With the following code you can easily retrieve the featured image for a page in WordPress with the following code. Display your page's featured image prominently on your website to improve your site's visual appeal and user experience. Implement this code to streamline your development process and enhance your website's functionality.
</p>
<span class="function">
<pre class="line-numbers">
<code class="language-php">if (has_post_thumbnail( $post->ID ) ): ?>
$image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'single-post-thumbnail');
echo $image[0];
endif;</code>
</pre>
</span>
</div>
</li>
<li id="ad">
<div>
<h3>
<span class="text">
# Get Blog Listing Featured Image
</span>
</h3>
<p class="topicDesc">
Retrieve the featured image for your WordPress blog listings with ease using the following code. Enhance your website's visual appeal and user experience by prominently displaying featured images for each blog post on your website. Simplify your development process and optimize your website's functionality with this useful code snippet.
</p>
<span class="function">
<pre class="line-numbers">
<code class="language-php">if (is_home() && get_option('page_for_posts') ) {
$img = wp_get_attachment_image_src(get_post_thumbnail_id(get_option('page_for_posts')),'full');
$featured_image = $img[0];
$featured_image;
}</code>
</pre>
</span>
</div>
</li>
<li id="ae">
<div>
<h3>
<span class="text">
# Get categories on single post
</span>
</h3>
<p class="topicDesc">
With the following code, you can retrieve the categories for a single post in WordPress. Streamline your website's navigation and improve user experience by displaying relevant categories for each post. Implement this code to enhance your website's functionality and optimize your WordPress development process.
</p>
<span class="function">
<pre class="line-numbers">
<code class="language-php">//Get categories on single post
global $product;
$terms = get_the_terms( $product->get_id(), 'product_cat' );
foreach ( $terms as $term ) {
echo $term->name;
}
</code>
</pre>
</span>
</div>
</li>
<li id="af">
<div>
<h3>
<span class="text">
# Get tags on single post
</span>
</h3>
<p class="topicDesc">
Retrieve the tags for a single post in WordPress with ease using the following code. Improve your website's navigation and user experience by displaying relevant tags for each post. Simplify your WordPress development process and enhance your website's functionality with this useful code snippet.
</p>
<span class="function">
<pre class="line-numbers">
<code class="language-php">//Get tags on single post
global $product;
$terms = get_the_terms( $product->get_id(), 'product_tags' );
foreach ( $terms as $term ) {
echo $term->name;
}
</code>
</pre>
</span>
</div>
</li>
<li id="ag">
<div>
<h3>
<span class="text">
# Adding Wordpress Menu
</span>
</h3>
<p class="topicDesc">
Retrieve a registered WordPress menu with ease using the following code. Display your menu on your website to improve navigation and user experience. Simplify your WordPress development process and enhance your website's functionality with this useful code snippet.
</p>
<span class="function">
<pre class="line-numbers">
<code class="language-php">//first need to register menu in function file
function register_codesnippets_menu() {
register_nav_menu('header-menu',__( 'header' ));
register_nav_menu('footer-menu',__( 'footer' ));
}
add_action( 'init' , 'register_codesnippets_menu' );
// add this code where you want to show your menu
wp_nav_menu(
array(
'theme_location' => 'header',
'menu' => 'header-menu',
'menu_class' => 'navbar-nav',
'menu_id' => 'primary-menu',
'container_class' => 'menu-inner',
)
)
</code>
</pre>
</span>
</div>
</li>
<li id="ag1">
<div>
<h3>
<span class="text">
# Add class in menu li.
</span>
</h3>
<p class="topicDesc">
Add a custom class to the li element in your WordPress menu with the following code. Enhance your website's design and user experience by customizing your menu with unique classes. Implement this code to optimize your WordPress development process and improve your website's functionality.
</p>
<span class="function">
<pre class="line-numbers">
<code class="language-php">//add class in li
add_filter ( 'nav_menu_css_class', 'add_class_to_menu_list', 10, 4 );
function add_class_to_menu_list ( $classes, $item, $args, $depth ){
if ($args->theme_location == 'header') {
/**** according to theme location *****/
$classes[] = 'gc-nav-item';
}
if ($args->theme_location == 'sidebar') {
/**** according to theme location *****/
$classes[] = 'gc-sidebar-item';
}
return $classes;
}
</code>
</pre>
</span>
</div>
</li>
<li id="ag2">
<div>
<h3>
<span class="text">
# Add class in menu anchor.
</span>
</h3>
<p class="topicDesc">
Add a custom class to the anchor element in your WordPress menu using the following code. Improve your website's design and user experience by customizing your menu with unique classes. Simplify your WordPress development process and enhance your website's functionality with this useful code snippet.
</p>
<span class="function">
<pre class="line-numbers">
<code class="language-php">//add class in anchor
add_filter( 'nav_menu_link_attributes', 'add_class_to_list_anchor', 10, 4 );
function add_class_to_list_anchor($atts, $item, $args, $depth) {
if ($args->theme_location == 'header') {
/**** according to theme location *****/
$atts['class'] = "gc-nav-link";
}
return $atts;
}
</code>
</pre>
</span>
</div>
</li>
<li id="ag3">
<div>
<h3>
<span class="text">
# Overide Submenu Class
</span>
</h3>
<p class="topicDesc">
Override the default submenu class in your WordPress menu with the following code. Customize your website's design and user experience by creating unique submenu classes. Implement this code to streamline your WordPress development process and optimize your website's functionality.
</p>
<span class="function">
<pre class="line-numbers">
<code class="language-php">//overide submenu class
add_filter( 'nav_menu_submenu_css_class', 'add_sub_menu_class', 10, 4);
function add_sub_menu_class($atts, $args, $depth) {
if ($args->theme_location == 'sidebar') {
/**** according to theme location *****/
return array('gc-sidebar-submenu');
}
}
</code>
</pre>
</span>
</div>
</li>
<li id="ag4">
<div>
<h3>
<span class="text">
# Add caret for submenus
</span>
</h3>
<p class="topicDesc">
Add a caret to your WordPress submenu using the following code. Improve your website's navigation and user experience by providing a visual cue for submenus. Simplify your WordPress development process and enhance your website's functionality with this useful code snippet.
</p>
<span class="function">
<pre class="line-numbers">
<code class="language-php">// add caret for submenus
function aiken_menu_arrow($item_output, $item, $depth, $args) {
if (in_array('menu-item-has-children', $item->classes)) {
$arrow = '<span class="subMenuAngle"> <i class="fas fa-chevron-down"></i></span>'; // Change the class to your font icon
$item_output = str_replace('</a>', '</a>'. $arrow .'', $item_output);
}
return $item_output;
}
add_filter('walker_nav_menu_start_el', 'aiken_menu_arrow', 10, 4);
</code>
</pre>
</span>
</div>
</li>
<li id="ah">
<div>
<h3>
<span class="text">
# Register Sidebar
</span>
</h3>
<p class="topicDesc">
With the following code, you can register a custom sidebar in WordPress. Customize your website's design and user experience by creating unique sidebars for specific pages or categories. Implement this code to optimize your WordPress development process and improve your website's functionality.
</p>
<span class="function">
<pre class="line-numbers">
<code class="language-php">//Register Sidebar in function.php
register_sidebar(
array(
'name' => _x('Mail Widget', 'backend', 'cargopress-pt'),
'id' => 'mail-widgets',
'description' => _x('Header area for Mail ID.', 'backend', 'cargopress-pt'),
'before_widget' => '<div class="widget %2$s">',
'after_widget' => '</div>',
)
);
// Show on frontend
if (is_active_sidebar('mail-widgets')) {
dynamic_sidebar('mail-widgets');
}
</code>
</pre>
</span>
</div>
</li>
<li id="ai">
<div>
<h3>
<span class="text">
# Create Shorcode
</span>
</h3>
<p class="topicDesc">
Create a custom shortcode in WordPress using the following code. Simplify your website development process by creating a shortcode that can be easily used throughout your website. Enhance your website's functionality and optimize your WordPress development process with this useful code snippet.
</p>
<span class="function">
<pre class="line-numbers">
<code class="language-php">//Create Shorcode
function shortcode_function($sc_atts){
$sc_default = array(
'id' => '0',
);
$sc_page_id = shortcode_atts($sc_default, $sc_atts);
// your code goes here or include any file
}
add_shortcode( 'shortcode_name' , 'shortcode_function' );
</code>
</pre>
</span>
</div>
</li>
<!--
<li id="aj">
<div>
<span class="text">
Get categories of Custom Post
</span>
<span class="function">
<pre class="line-numbers">
<code class="language-php">//Get categories of custom post
$args = array(
'type' => 'casestudy',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'taxonomy' => 'categories',
'pad_counts' => false );
$categories = get_categories($args);
<ul>
<li>
<a class="active" data-filter="*" href="javascript:void(0)">
All Market
</a>
</li>
<?php
foreach ($categories as $category) {
$url = get_term_link($category);?>
<li>
<a class="active" data-filter=".<?php echo $url;?>" href="javascript:void(0)">
<?php echo $category->name; ?>
</a>
</li>
<?php } ?>
</ul>
</code>
</pre>
</span>
</div>
</li>
-->
<li id="aj">
<div>
<h3>
<span class="text">
# Get categories and subcategories of post
</span>
</h3>
<p class="topicDesc">
Retrieve the categories and subcategories of a post in WordPress with the following code. Streamline your website's navigation and improve user experience by displaying relevant categories and subcategories for each post. Implement this code to enhance your website's functionality and optimize your WordPress development process.
</p>
<span class="function">
<pre class="line-numbers">
<code class="language-php">//Get categories and subcategories of post
$outlet_type = get_terms(["taxonomy" => "product_cat", "parent" => 0]);
foreach($outlet_type as $outlet){
$terms = get_terms(
array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
'parent' => $outlet->term_id
)
);
$html .='<span data-id="'.$outlet->term_id.'" value="'.$outlet->name.'">'.$outlet->name.'</span>';
foreach ( $terms as $term ) {
$html .='<span data-id="'.$term->term_id.'" value="'.$term->name.'">'.$term->name.'</span>';
}
}
echo $html;
</code>
</pre>
</span>
</div>
</li>
<li id="ak1">
<div>
<h3>
<span class="text">
# Redirect after Login & Registration
</span>
</h3>
<p class="topicDesc">
Redirect users to a specific page after login or registration with the following code. Enhance your website's user experience by providing a customized redirect page for your users. Implement this code to optimize your WordPress development process and improve your website's functionality.
</p>
<span class="function">
<pre class="line-numbers">
<code class="language-php">//Redirect user after login and registration to shop page
function ap_login_redirect_wc( $redirect, $user ) {
return home_url('/shop');
}
add_action('woocommerce_login_redirect', 'ap_login_redirect_wc', 10, 2);
function ap_registration_redirect_wc() {
// wp_logout();
return home_url('/shop');
}
add_action('woocommerce_registration_redirect', 'ap_registration_redirect_wc', 2);
</code>
</pre>
</span>
</div>
</li>
<li id="ak2">
<div>
<h3>
<span class="text">
# Custom Woocomerce Login
</span>
</h3>
<p class="topicDesc">
Customize the login functionality of your WooCommerce store with the following code. Enhance your website's user experience and branding by customizing the WooCommerce login form. Implement this code to optimize your WordPress development process and improve your website's functionality.
</p>
<span class="function">
<pre class="line-numbers">
<code class="language-php">/* Shortcode for woocommerce login */
add_shortcode( 'wc_login_form_jags', 'jags_separate_login_form' );
function jags_separate_login_form() {
if ( is_admin() ) return;
if ( is_user_logged_in() ) return;
ob_start();
woocommerce_login_form( array( 'redirect' => 'https://socialx22.sg-host.com/' ) );
return ob_get_clean();
}
</code>
</pre>
</span>
</div>
</li>
<li id="ak3">
<div>
<h3>
<span class="text">
# Custom Woocommerce Login & Register Form
</span>
</h3>
<p class="topicDesc">
Create a custom WooCommerce login and registration form with the following code. Improve your website's user experience and branding by creating a unique login and registration form. Simplify your WordPress development process and enhance your website's functionality with this useful code snippet.
</p>
<span class="function">
<pre class="line-numbers">
<code class="language-php">
<?php
/*** add this code in funtion.php ***/
/**
* Custom Login Shortcode For Home 2
**/
add_shortcode( 'wc_login_form_jags', 'jags_separate_login_form' );
function jags_separate_login_form() {
if ( is_admin() ) return;
if ( is_user_logged_in() ) return;
ob_start();
woocommerce_login_form( array( 'redirect' => 'https://your-redirect-url.com/' ) );
return ob_get_clean();
}
/**
* @snippet WooCommerce User Registration Shortcode
* @author Jags
*/
add_shortcode( 'wc_reg_form_jags', 'jags_woo_registration_form' );
function jags_woo_registration_form() {
if ( is_admin() ) return;
if ( is_user_logged_in() ) return;
ob_start();
// NOTE: THE FOLLOWING <FORM></FORM> IS COPIED FROM woocommerce\templates\myaccount\form-login.php
// IF WOOCOMMERCE RELEASES AN UPDATE TO THAT TEMPLATE, YOU MUST CHANGE THIS ACCORDINGLY
do_action( 'woocommerce_before_customer_login_form' );
?>
<form method="post" class="woocommerce-form woocommerce-form-register register" <?php do_action( 'woocommerce_register_form_tag' ); ?> >
<?php do_action( 'woocommerce_register_form_start' ); ?>
<?php if ( 'no' === get_option( 'woocommerce_registration_generate_username' ) ) : ?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_username"><?php esc_html_e( 'Username', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="username" id="reg_username" autocomplete="username" value="<?php echo ( ! empty( $_POST['username'] ) ) ? esc_attr( wp_unslash( $_POST['username'] ) ) : ''; ?>" /><?php // @codingStandardsIgnoreLine ?>
</p>
<?php endif; ?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_email"><?php esc_html_e( 'Email address', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="email" class="woocommerce-Input woocommerce-Input--text input-text" name="email" id="reg_email" autocomplete="email" value="<?php echo ( ! empty( $_POST['email'] ) ) ? esc_attr( wp_unslash( $_POST['email'] ) ) : ''; ?>" /><?php // @codingStandardsIgnoreLine ?>
</p>
<?php if ( 'no' === get_option( 'woocommerce_registration_generate_password' ) ) : ?>
<p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
<label for="reg_password"><?php esc_html_e( 'Password', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="password" class="woocommerce-Input woocommerce-Input--text input-text" name="password" id="reg_password" autocomplete="new-password" />
</p>
<?php else : ?>
<p><?php esc_html_e( 'A password will be sent to your email address.', 'woocommerce' ); ?></p>
<?php endif; ?>
<?php do_action( 'woocommerce_register_form' ); ?>
<p class="woocommerce-FormRow form-row">
<?php wp_nonce_field( 'woocommerce-register', 'woocommerce-register-nonce' ); ?>
<button type="submit" class="woocommerce-Button woocommerce-button button woocommerce-form-register__submit" name="register" value="<?php esc_attr_e( 'Register', 'woocommerce' ); ?>"><?php esc_html_e( 'Register', 'woocommerce' ); ?></button>
</p>
<?php do_action( 'woocommerce_register_form_end' ); ?>
</form>
<?php
return ob_get_clean();
}
?>
</code>
</pre>
</span>
</div>
</li>
<li id="ak4">
<div>
<h3>
<span class="text">
# Logout from Woocommerce
</span>
</h3>
<p class="topicDesc">
Allow users to log out from your WooCommerce store with the following code. Improve your website's user experience and security by providing a logout functionality for your users. Implement this code to optimize your WordPress development process and improve your website's functionality.
</p>
<span class="function">
<pre class="line-numbers">
<code class="language-html"><!-- Logout Link (add in href) -->
<a href="./my-account/customer-logout/?_wpnonce=2bbbac43a8&customer-logout=true">Logout</a>
</code>
</pre>
</span>
</div>
</li>
<li id="ak5">
<div>
<h3>
<span class="text">
# Remove links of single products from products listing
</span>
</h3>
<p class="topicDesc">
Remove the links of single products from the products listing in WooCommerce with the following code. Customize the design of your product listing page by removing unwanted links. Implement this code to optimize your WordPress development process and improve your website's functionality.
</p>
<span class="function">
<pre class="line-numbers">
<code class="language-php">/*** remove single products link ***/
remove_action('woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10);
remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5);
/*** redirect single product page to shop page ****/
add_action('wp', 'ts_redirect_product_pages', 99);
function ts_redirect_product_pages()
{
if (is_product()) {
wp_safe_redirect(home_url('/shop'));
exit;
}
}
</code>
</pre>
</span>
</div>
</li>
<li id="ak6">
<div>
<h3>
<span class="text">
# Remove/Reduce Password Security
</span>
</h3>
<p class="topicDesc">
Remove or reduce the password security for your website with the following code. Simplify your website's login process and improve user experience. Implement this code to enhance your website's functionality and optimize your WordPress development process.
</p>
<span class="function">
<pre class="line-numbers">
<code class="language-php">/*** reduce password security ***/
function codesnippets_min_password_strength( $strength ) {
return 2;
}
add_filter( 'woocommerce_min_password_strength', 'codesnippets_min_password_strength', 10, 1 );
/*** reduce password security ***/
function codesnippets_remove_password_strength() {
wp_dequeue_script( 'wc-password-strength-meter' );
}
add_action( 'wp_print_scripts', 'codesnippets_remove_password_strength', 10 );
</code>
</pre>
</span>
</div>
</li>
<li id="al">
<div>
<h3>
<span class="text">
# Get file size
</span>
</h3>
<p class="topicDesc">
Retrieve the file size when using the Advanced Custom Fields plugin in WordPress with the following code. Enhance your website's functionality and user experience by displaying file size information. Implement this code to optimize your WordPress development process.
</p>
<span class="function">
<pre class="line-numbers">
<code class="language-php">//Get file size of upladed file using ACF (this method can also used for normal condition)
while( have_rows('ap-zips') ) : the_row();
$images = get_sub_field('photos_zip' );
$fileSize = $images['filesize'];
$fileSize = size_format($fileSize, 2);
echo $images['url'];
echo $fileSize;
endwhile;
</code>
</pre>
</span>
</div>
</li>
<li id="am">
<div>
<h3>
<span class="text">
# Upload Files from Fronend in wordpress using Ajax
</span>
</h3>
<p class="topicDesc">
Allow users to upload files from the frontend of your WordPress website using Ajax with the following code. Improve your website's user experience and functionality by providing a convenient way for users to upload files. Implement this code to optimize your WordPress development process.
</p>
<span class="function">
<pre class="line-numbers">
<code class="language-php">//php part put this code in function.php
function blog_scripts() {
// Register the script
wp_register_script( 'custom-script', get_stylesheet_directory_uri(). '/js/custom.js', array('jquery'), false, true );
// Localize the script with new data
$script_data_array = array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'security' => wp_create_nonce( 'file_upload' ),
);