List of tutorial
1. How to create contact form with submit mail
2. How to insert record post database table
3. How to insert shortcode google ads
4. How to list recent post
5. How to list recent post in shortcode parameters
6. How to list recent post in content shortcode
7. How to enabling shortcodes in widgets
8. Display wordpress menu in shortcode
9. How to insert A Google Maps shortcode
10. How to embed pdf
11. How to Displaying database table data in shortcodes
A standard header is introduced below to establish your plugin’s presence.
<?php /** * Plugin Name: My Plugin Name * Plugin URI: http://mypluginuri.com/ * Description: A brief description about your plugin. * Version: 1.0 or whatever version of the plugin (pretty self explanatory) * Author: Plugin Author's Name * Author URI: Author's website * License: A "Slug" license name e.g. GPL12 */Create folder under plugins folder wp-content\plugins\myplugins_with_shortcode folder name myplugins_with_shortcode and create php file index.php file
2. Add the code below wp-content\plugins\myplugins_with_shortcode\index.php
<?php /** * Plugin Name: Custom Plugins with shortcode * Plugin URI: https://tutorial101.blogspot.com/ * Description: A custom plugin and shortcode for example. * Version: 1.0 * Author: Cairocoders * Author URI: http://tutorial101.blogspot.com/ **/ //first example how to create contact form with submit mail function form_creation(){ //add submit if (isset( $_POST['cmdsubmit'])) { $txtname = $_POST['txtname']; $txtemail = $_POST['txtemail']; $message = $_POST['message']; $msg = "Name: $txtname <br/>Email: $txtemail <br/>Message: $message <br/>"; // send email mail("someone@example.com","My subject",$msg); } $form .= '<div class="well"><p><h5>Simple Contact Form</h5></p><form method = "post"> Name: <input type="text" name="txtname" class="form-control"><br> Email: <input type="email" name="txtemail" class="form-control"><br> Message: <textarea name="message" class="form-control"> Enter text here…</textarea><br/> <input type="submit" name="cmdsubmit"> '.$msg.' </form></div>'; return "$form"; } add_shortcode("testform", "form_creation"); //add code //<p>[testform]</p> //add css file function wpse_89494_enqueue_scripts() { wp_enqueue_style('handlecss', plugin_dir_url(__FILE__) . 'css/ajaxshortcodestyle.css'); } //second example how to insert to post function input_fields( $atts ) { //add submit if ( isset( $_POST['gg'] ) ) { $post = array( 'post_content' => $_POST['content'], 'post_status' => 'publish', 'post_title' => $_POST['title'] ); // Insert the post into the database wp_insert_post($post); //https://developer.wordpress.org/reference/functions/wp_insert_post/ } $formpost .= '<form method = "post"> Post title : <input type="text" name="title"><br/> Post Contents : <input type="text" name="content"></br/> <input type="submit" name="gg"> </form>'; return "$formpost"; } add_shortcode( 'add_fields', 'input_fields' ); //third example shortcode google ads function get_adsense($atts) { return '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- WordPress hosting --> <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-0327511395451286" data-ad-slot="6566424785"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script>'; } add_shortcode('adsense', 'get_adsense'); //4th example how to list recent post function recent_posts_function() { query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1)); if (have_posts()) : while (have_posts()) : the_post(); $return_string = '<a href="'.get_permalink().'">'.get_the_title().'</a>'; endwhile; endif; wp_reset_query(); return $return_string; } add_shortcode('recent_posts', 'recent_posts_function'); //5 example SHORTCODE PARAMETERS function recent_posts_function2($atts){ extract(shortcode_atts(array( 'posts' => 1, ), $atts)); $return_string = '<ul>'; query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts)); if (have_posts()) : while (have_posts()) : the_post(); $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>'; endwhile; endif; $return_string .= '</ul>'; wp_reset_query(); return $return_string; } add_shortcode('recent_posts2', 'recent_posts_function2'); //6 example CONTENT IN SHORTCODE [recent-posts posts="5"]This is the list heading[/recent-posts] function recent_posts_function3($atts, $content = null) { extract(shortcode_atts(array( 'posts' => 1, ), $atts)); $return_string = '<h3>'.$content.'</h3>'; $return_string .= '<ul>'; query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts)); if (have_posts()) : while (have_posts()) : the_post(); $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>'; endwhile; endif; $return_string .= '</ul>'; wp_reset_query(); return $return_string; } add_shortcode('recent_posts3', 'recent_posts_function3'); //7 example ENABLING SHORTCODES IN WIDGETS add_filter('widget_text', 'do_shortcode'); //8 example WORDPRESS MENU function menu_function($atts, $content = null) { extract( shortcode_atts( array( 'name' => null, ), $atts ) ); return wp_nav_menu( array( 'menu' => $name, 'echo' => false ) ); } add_shortcode('menu', 'menu_function'); //9 example A Google Maps shortcode function googlemap_function($atts, $content = null) { extract(shortcode_atts(array( "width" => '640', "height" => '480', "src" => ’ ), $atts)); return '<iframe width="'.$width.'" height="'.$height.'" src="'.$src.'&output=embed" ></iframe>'; } add_shortcode("googlemap", "googlemap_function"); //10 example PDF EMBEDDING function pdf_function($attr, $url) { extract(shortcode_atts(array( 'width' => '640', 'height' => '480' ), $attr)); return '<iframe src="http://docs.google.com/viewer?url=' . $url . '&embedded=true" style="width:' .$width. '; height:' .$height. ';">Your browser does not support iframes</iframe>'; } add_shortcode('pdf', 'pdf_function'); // last 11 example Displaying database table data in shortcodes function listpost_func() { global $wpdb; $postshow .= '<div class="recent-posts">'; $sql_post = $wpdb->get_results("SELECT * FROM wp_posts WHERE post_status='publish'"); foreach ($sql_post as $rows_post) { $post_title = $rows_post->post_title; $postshow .= '<h6>'.$post_title.'</h6>'; } $postshow .= '</div>'; return "$postshow"; } add_shortcode( 'listpost', 'listpost_func' );css code wp-content\plugins\myplugins_with_shortcode\css\ajaxshortcodestyle.css
.form-control { display: block; width: 100%; height: 34px; padding: 6px 12px; font-size: 14px; line-height: 1.428571429; color: #555; background-color: #fff; background-image: none; border: 1px solid #ccc; border-radius: 4px; transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s; } .btn-primary { color: #fff; background-color: #428bca; border-color: #357ebd; } .well { padding: 19px; margin-bottom: 20px; background-color: #428bca; border: 1px solid #357ebd;color:#fff; border-radius: 4px; -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.05); box-shadow: inset 0 1px 1px rgba(0,0,0,.05); }