Wordpress short code plugin website design

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/leon_zeng0/article/details/89137518

WordPress shortcode refers to the use of some [] short code embodied, WordPress recognizes these short codes and to define the output short code in accordance with a specific content, Shortcode API This feature is WordPress from version 2.5 were introduced in use it can to a web and the contents of the log to add a variety of functions, and Shortcode this interface is very easy to use and very powerful.

Simple short code can replace simple content, can be as complex Contact Form 7 such complex forms.

Short codes can be added function.php file can also be added as plug-ins.

Here we have a way to add plug-ins. Because of the need to add files, directories, if not the local test, you can use wordpress plugin panel file manager or FTP.

The first short code plugin

In Wordpress directory, wp_content, and then create a directory plugins directory exampleplugin follows:

C:\wamp64\www\wordpress\wp-content\plugins\exampleplugin

Exampleplugin.php build a file in this directory

Document reads as follows:

<?php
/*
*Plugin Name: example short code plugins
* Description: This is just an example plugins.
*/

// Example 2 : WP Shortcode to display text on page or post.
function wp_first_shortcode(){
return "<br>This is a very simple short code<br>";
}
add_shortcode('firstshortcode', 'wp_first_shortcode');

Our plug-in code is complete.

Wordpress came to the panel, we can see our control, the name is example short code plugins:

Activate him, Activate.

Now we can test to verify our first short code.

Create a Web page, or open an existing web page to add [firstshortcode] as follows:

We save, update, view and then we look at the content of our web page, a red box is our output in the short code in the function.

 Short codes with parameter plug

Now the file code amended as follows:

<?php
/*
*Plugin Name: example short code plugins
* Description: This is just an example plugins.
*Plugin URI: http://www.liwensoft.com
*Version: 0.1
*Author: Denis
*/

// Example 2 : WP Shortcode to display text on page or post.
function wp_first_shortcode(){
return "This is a very simple short code";
}
add_shortcode('firstshortcode', 'wp_first_shortcode');

//with parameter
function shortcode_with_parameter_handler($atts) {
	$a = shortcode_atts( array(
		'foo' => 'attribute 1 default',
		'bar' => 'attribute 2 default',
		// ...etc
	), $atts );

	$info= " this is a short code with parameter<br>foo=";
	$info.= $a['foo'];
	$info.="<br> bar=";
	$info.= $a['bar'];
		return $info;
}
add_shortcode('secondshortcodewithparameter', 'shortcode_with_parameter_handler');

Edit pages as follows: 

first short code

[firstshortcode]

second shortcode

[secondshortcodewithparameter]

after short code

Web page updates and displays as follows:

Because we do not enter parameters in a short line of code, it displays the default parameters. The second short code an input parameter,

[secondshortcodewithparameter foo="foo"]

It is displayed as follows:

this is a short code with parameter
foo=foo
bar=attribute 2 default

Two input parameters, as follows:

[secondshortcodewithparameter foo="foo para" bar="bar para"]

It is displayed as:

this is a short code with parameter
foo=foo para
bar=bar para

More content parameters, can refer to: https://codex.wordpress.org/Shortcode_API 

Short codes with external parameters widget

Short code can use external parameters, now we add a short code function, examples demonstrate the use of external parameters, as follows:

function wp_para_shortcode(){
	$info=" this is a short code with global varias<br> file directory=";
	$info .=$_SERVER['DOCUMENT_ROOT'];
	$info .="<br> page para= ";
	$info .=$_GET['q'];
return $info;
}
add_shortcode('wpparashortcode', 'wp_para_shortcode');

Look at the situation to run the tests:

No input parameter q in the page, the parameter is an empty page (page para =):

This page parameter input? Q = www.liwensoft.com, so when the page is displayed, the parameter is www.liwensoft.com, change parameters, page content has changed. I want to be a download page, q enter the file name, then you can get the path to the file, change the file last download.

Short Code plugin introduced here. When tested, it is easy to mistake a character, leads to failure, especially Chinese single quotes, double quotes.

For your convenience test, the last put the code here:

<?php
/*
*Plugin Name: example short code plugins
* Description: This is just an example plugins.
*Plugin URI: http://www.liwensoft.com
*Version: 0.1
*Author: Denis
*/

// Example 2 : WP Shortcode to display text on page or post.
function wp_first_shortcode(){
return "This is a very simple short code";
}
add_shortcode('firstshortcode', 'wp_first_shortcode');

//with parameter
function shortcode_with_parameter_handler($atts) {
	$a = shortcode_atts( array(
		'foo' => 'attribute 1 default',
		'bar' => 'attribute 2 default',
		// ...etc
	), $atts );

	$info= " this is a short code with parameter<br>foo=";
	$info.= $a['foo'];
	$info.="<br> bar=";
	$info.= $a['bar'];
		return $info;
}
add_shortcode('secondshortcodewithparameter', 'shortcode_with_parameter_handler');

function wp_para_shortcode(){
	$info=" this is a short code with global varias<br> file directory=";
	$info .=$_SERVER['DOCUMENT_ROOT'];
	$info .="<br> page para= ";
	$info .=$_GET['q'];
return $info;
}
add_shortcode('wpparashortcode', 'wp_para_shortcode');

 

Guess you like

Origin blog.csdn.net/leon_zeng0/article/details/89137518