Wordpress Tutorials
- Wordpress
- Directory and file structure.
- Wordpress configuration
- Htaccess Configuration
- WP-CONTENT user playground
- Increase file size loading in phpmyadmin on localhost.
- Fatal error: Allowed memory size of x bytes
- wordpress Dashboard
- How to set local host domain or Virtual host
- Header, index and footer page.
- How to create wordpress Themes
- Html to wordpress themes
- Child Themes
- Increase Upload Max Filesize
- wp_enqueue_style & script
- How to make plugin
- Create Plugin
- Insert data into table using plugin.
- Show data from database
- Hook & Filter
- Template Or Custom page template
- Custom Login template
- How to show post on diffrent page.
- Custom widgit
- How to use contact form 7 plugin.
- Wordpress Database
- How to use Duplicator plugin
- How to fix Fatal Error: Maximum Execution Time Exceeded in WordPress
- How to debuggin mode on in wordpress
- Auto update plugin and themes
- How to Transfer WordPress
- How to change a WordPress site URL
- Top 10 issues in wordpress
- wordpress website link
Wordpress setting
Wordpress Function
wordpress top 10 problems
Important Links
WordPress Plugin:
A WordPress Plugin is a program, or a set of one or more functions, written in the PHP scripting language, that adds a specific set of features or services to the WordPress weblog, which can be seamlessly integrated with the weblog using access points and methods provided by the WordPress Plugin Application Program Interface (API).
Creating a Plugin :-
you will need to first determine its name. Some developers choose a name that describes more of what the plugin does so the end user can establish a quick connection with the name.
Plugin Files
Typically a plugin lives within its own folder under wp-content/plugins/ inside your WordPress installation. There is usually at least one PHP file that is typically named after the plugin. So if your plugin was named amazing-plug then your PHP file name would most likely be amazing-plug.php. Using a unique name is crucial so no two plugins use the same name.
Standard Plugin File :-
A plugin must contain a bit of meta information which tells WordPress what it is and how to handle it within your website. Plugins can be installed, deleted, activated, and inactivated. A standard header is introduced below to establish your plugin’s presence. The parameters shown will tell WordPress how to optimize it within your website and WordPress admin area.
Readme File
Readme files are useful for other developers and users. Usually these files give a quick description of the plugin as well as sometimes offer change logs which indicate previous updates and maintenance announcements to users.
File name : index.php
<?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
*/
The minimum WordPress needs to establish your file as a plugin is the line
Plugin Name: My Plugin Name
The rest of the information will be displayed within the Admin area under the Pluginssection.
How to create a plugin
First go on D:\xampp\htdocs\Your project\wp-content\plugins
Create a folder myplugin (Whose name is defined by the user).
And create a php file on that folder such as myuser.php
With your new file created we need to add some parameters in comment.
File name : index.php
<?php
/**
* Plugin Name: myuser-plugin
* Plugin URI: http://elegantthemes.com/
* Description: A custom music review plugin built for example.
* Version: 1.0
* Author: Andy Leverenz
* Author URI: http://justalever.com/
**/
?>
With this information added, save your file and navigate to your WordPress admin area. Click on Plugins on the left side navigation and you should now see our plugin available
Create contact form plugin
All WordPress pluigns are PHP files, located in the /wp-content/plugins/ directory. In our example, the file will be called myform-example.php.
<?php
/* Plugin Name: Example Contact Form Plugin Plugin URI: http://example.com Description: Simple non-bloated WordPress Contact Form Version: 1.0 Author: Agbonghama Collins Author URI: http://w3guy.com
*/
//
// the plugin code will go here..
//
?>
File name : index.php
Next, we add the function html_form_code() that contains the contact form HTML:
function html_form_code()
{
echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">';
echo '<p>';
echo 'Your Name (required) <br />';
echo '<input type="text" name="cf-name" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["cf-name"] ) ? esc_attr( $_POST["cf-name"] ) : '' ) . '" size="40" />';
echo '</p>'; echo '<p>';
echo 'Your Email (required) <br />';
echo '<input type="email" name="cf-email" value="' . ( isset( $_POST["cf-email"] ) ? esc_attr( $_POST["cf-email"] ) : '' ) . '" size="40" />';
echo '</p>'; echo '<p>'; echo 'Subject (required) <br />'; echo '<input type="text" name="cf-subject" pattern="[a-zA-Z ]+" value="' . ( isset( $_POST["cf-subject"] ) ? esc_attr( $_POST["cf-subject"] ) : '' ) . '" size="40" />';
echo '</p>'; echo '<p>'; echo 'Your Message (required) <br />';
echo '<textarea rows="10" cols="35" name="cf-message">' . ( isset( $_POST["cf-message"] ) ? esc_attr( $_POST["cf-message"] ) : '' ) . '</textarea>';
echo '</p>';
echo '<p><input type="submit" name="cf-submitted" value="Send"/></p>';
echo '</form>';
}
Basic validation was added to the form via the pattern input attribute.
The RegEX in the contact form does the following field validation:
[a-zA-Z0-9 ]: only letters, spaces and numbers allowed in the name field; special symbols are deemed invalid.
[a-zA-Z ]: only letters and spaces are allowed in the subject field.
The email form control validates the email field hence there is no need for a pattern attribute.
The function deliver_mail() sanitizes the form data and sends the mail to the WordPress administrator’s email address.
File name : index.php
function deliver_mail()
{
// if the submit button is clicked, send the email
if ( isset( $_POST['cf-submitted'] ) )
{ // sanitize form values
$name = sanitize_text_field( $_POST["cf-name"] ); $email = sanitize_email( $_POST["cf-email"] ); $subject = sanitize_text_field( $_POST["cf-subject"] ); $message = esc_textarea( $_POST["cf-message"] ); // get the blog administrator's email address $to = get_option( 'admin_email' ); $headers = "From: $name <$email>" . "\r\n"; // If email has been process for sending, display a success message if ( wp_mail( $to, $subject, $message, $headers ) ) { echo '<div>'; echo '<p>Thanks for contacting me, expect a response soon.</p>'; echo '</div>'; } else { echo 'An unexpected error occurred';
}
} }
The sanitation of the form data is done by the following WordPress internal functions:
sanitize_text_field(): Sanitize the data from user input.
sanitize_email(): Strips out all characters that are not allowable in an email.
esc_textarea(): Escape the message text area values.
The code get_option( 'admin_email' ) programmatically retrieves the WordPress administrator’s email address from the database where the email will be delivered to.
Don’t want the contact form to send the mail to admin? Simply set the variable $to to the desired email address.
The function cf_shortcode() is the callback function that is called when the contact form shortcode [sitepoint_contact_form] is active.
function cf_shortcode()
{
ob_start();
deliver_mail();
html_form_code();
return ob_get_clean();
}
The above functions calls the html_form_code() and deliver_mail() functions to display the contact form HTML form and validate the form data respectively.
Finally, the shortcode [my_contact_form] is registered to WordPress. So simply add:
add_shortcode( 'my_contact_form', 'cf_shortcode' );
write [my_contact_form] on your wp page.
Complete Code :-
File name : index.php
<?php
/**
* Plugin Name: user-register
* 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
*/
function html_form_code()
{
echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">';
echo '<p>';
echo 'Your Name (required) <br />';
echo '<input type="text" name="cf-name" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["cf-name"] ) ? esc_attr( $_POST["cf-name"] ) : '' ) . '" size="40" />';
echo '</p>'; echo '<p>';
echo 'Your Email (required) <br />';
echo '<input type="email" name="cf-email" value="' . ( isset( $_POST["cf-email"] ) ? esc_attr( $_POST["cf-email"] ) : '' ) . '" size="40" />';
echo '</p>'; echo '<p>'; echo 'Subject (required) <br />'; echo '<input type="text" name="cf-subject" pattern="[a-zA-Z ]+" value="' . ( isset( $_POST["cf-subject"] ) ? esc_attr( $_POST["cf-subject"] ) : '' ) . '" size="40" />';
echo '</p>'; echo '<p>'; echo 'Your Message (required) <br />';
echo '<textarea rows="10" cols="35" name="cf-message">' . ( isset( $_POST["cf-message"] ) ? esc_attr( $_POST["cf-message"] ) : '' ) . '</textarea>';
echo '</p>';
echo '<p><input type="submit" name="cf-submitted" value="Send"/></p>';
echo '</form>';
}
function deliver_mail()
{
// if the submit button is clicked, send the email
if ( isset( $_POST['cf-submitted'] ) )
{ // sanitize form values
$name = sanitize_text_field( $_POST["cf-name"] );
$email = sanitize_email( $_POST["cf-email"] );
$subject = sanitize_text_field( $_POST["cf-subject"] );
$message = esc_textarea( $_POST["cf-message"] );
// get the blog administrator's email address
$to = get_option( 'admin_email' );
$headers = "From: $name <$email>" . "\r\n"; // If email has been process for sending, display a success message
if ( wp_mail( $to, $subject, $message, $headers ) )
{
echo '<div>'; echo '<p>Thanks for contacting me, expect a response soon.</p>'; echo '</div>';
}
else
{
echo 'An unexpected error occurred';
}
}
}
function cf_shortcode()
{
ob_start();
deliver_mail();
html_form_code();
return ob_get_clean();
}
add_shortcode( 'my_contact_form', 'cf_shortcode' );
?>
Output :-
write shortcut plugin key
[my_contact_form]
File name : index.php
File name : index.php