挑战
当前,WordPress是最流行的内容管理系统(CMS)之一。它可以用于创建个人博客,电子商务互联网商店,登录页面,小型企业或公司网站等等。客户喜欢它,因为它易于设置,有各种各样的插件,并且总的来说,它具有用户友好的方法。开发人员喜欢它,因为它具有易于理解的功能,这意味着设置起来很容易,但仍然给您不错的质量。
使用WordPress的站点数量正在增长,这对大量专用WordPress开发人员产生了需求。那么,如何区分新手和经验丰富的WordPress专家?在本指南中,我们提出了一些问题和答案,可以帮助您确定应聘者的技能水平。

当您可以雇用顶级WordPress开发人员并节省资金时,为什么还要花时间和金钱从头开始构建网站?
问题和解答
问:讨论主题自定义以及如何设置自定义主题,命名所需的最少文件,并说明WordPress如何识别新主题。
每个WordPress网站都需要一个主题。它是它的结构方式,因此是站点的组成部分。
The new theme is created as follows: In the wp-content/themes/
directory, create and name a new folder. Best practice is to use the name 的 the company for which you are making the 网站. Use lower case (Roman alphabet) without spaces. Note that you need to put in the theme folder these five files: style.css
, functions.php
, index.php
, header.php
and footer.php
. Later, you can extend the theme with other custom files.
The formation 的 the style.css
file header is a part 的 the base theme settings. With the help 的 file style.css
and headers in it, WordPress的’s core will recognize your folder as the new theme.
The beginning 的 the style.css
should be as follows:
/*
Theme Name: [theme name]
Author: [author name]
Author URI: [author URL]
Version: [theme version]
Description: [theme description]
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
如果创建子主题,则需要在指示基本主题的位置添加另一行。
Template: twentyfifteen
Where twentyfifteen
is the name 的 the main (parent) folder theme.
问:说明WordPress如何生成页面。
WordPress的 recognizes and analyzes the request entered in the URL bar, picks the best 的PHP file, and generates the webpage. It makes a global object $wp_query
based on the request in the URL. Then, with the help 的 its functions, WordPress的 loads other files, such as header.php
, footer.php
, and sidebar.php
.
检查 模板层次结构方案,它定义了将输出当前查询内容的过程和文件。
For example, if the address is /? Cat = 1
, WordPress的 will first look for category-slug.php
file. If the file is not found, it will search the category-id.php
file. If this can’t be found, either, it looks for the file category.php
, and so on, until it finds the appropriate file. If it can’t find one, it simply selects the index.php
file.
问:说明主题发展的语义和好的主题的基本原理。主题文件应保留什么?
以下是WordPress的一些最佳做法和创建新主题的推荐原则:
- The function
wp_head()
must be in the header.php
file.
- The function that operates classes, must be in the
header.php
file, inside the body tag: <body <php body_class ();? ? >>
- The function
wp_footer()
must be in the footer.php
file.
- If you have a repeating functionality, this code must be put into separate function, and this function must be added to the
functions.php
file.
- In all files, except
header.php
and the footer.php
, the number 的 opening HTML tags must be equal to the number 的 closing tags.
- If the file contains duplicate pieces 的 HTML code, it is better to unite them in a single file, put it in a theme subfolder, which can be named
blocks
or whatever you need, connect this piece 的 code, and call the function get_template_part( 'blocks/[name-of-the-file.php]' );
.
- File
index.php
is the final file to process the request, so it needs to have the main loop minus any queries. That means it will be impossible to alter the main loop in index.php
.
- All the theme settings must be in the
functions.php
file: 行动s, filters, custom functions, and custom classes. Although classes and functions may be located in different files, and be connected to the main functions.php
, or be placed in a separate plugin.
- 该代码不应包含注释代码。允许使用注释作为描述,但应删除设计中已使用的代码块。
- 文件中的代码应借助缩进进行组织。
- 主题必须由 w3c标准.
- The theme should work without exterior plug-ins. For example, if we use the
WP-PageNavi
plugin, you must write:
if (function_exists ( 'wp_pagenavi')):
wp_pagenavi ();
else:
get_template_part ( 'blocks / pager');
endif;
if (function_exists ('name_of_the_function'))
- For direct requests to the database, you must use the class
$wpdb
and its methods.
- If you use the form and need to create an options page admin panel, you must use
wp_nonce_field()
, and on the server side check this field and 行动:
<?php
if ( ! isset( $_POST['name_of_nonce_field'] ) || ! wp_verify_nonce( $_POST['name_of_nonce_field'], 'name_of_my_action' )
) {
print 'Sorry, your nonce did not verify.';
exit;
} else {
// process form data
}
?>
/* some code is here*/
<form method="post">
<!-- some inputs here ... -->
<?php wp_nonce_field( 'name_of_my_action', 'name_of_nonce_field' ); ?>
</form>
- If some files are not used, remove them from theme folder. If a client doesn’t need to use “search” on his site, remove
searchform.php
from the theme folder.
- Functions that begin with the
_
must only be written in the loop. And in the loop, if possible, use the functions started with the _
for presenting display fields.
问:说明如何创建自定义页面模板。
对于自定义页面模板,最好创建一个单独的文件夹。不要将自定义模板与现有的WordPress模板混合使用。将自定义模板文件放入此文件夹。在文件的开头,您需要添加以下代码:
<? Php
/ *
Template Name: [Template_Name]
* /
get_header (); ?>
问:说明如何将CSS和JavaScript文件正确包含到主题或插件中。
You can use <link>
or <script>
, but the correct way is to connect them in the functions.php
file. You need to add 行动 to wp_enqueue_scripts
, and our 行动 is to connect 的JavaScript and 的CSS files:
function theme_scripts_styles () {
// Connection 的 a custom JS file after jquery library that will be connected independently 通过 WordPress的
wp_enqueue_script ( 'theme-script', get_template_directory_uri () '/js/jquery.main.js', array ( 'jquery').);
// Loads default theme style.css.
wp_enqueue_style ( 'theme-style', get_stylesheet_uri (), array ());
// Connection 的 a custom css file
wp_enqueue_style (. 'theme-theme', get_template_directory_uri () '/theme.css', array ());
}
add_action( 'wp_enqueue_scripts', 'theme_scripts_styles' );
问:说明如何制作自定义菜单以及如何在页面上显示该菜单。
要显示菜单,您必须:
- Select the location for ther menu in the
functions.php
:
register_nav_menus (array (
'Primary' => __ ( 'Primary Navigation'),
));
if (has_nav_menu ( 'primary'))
wp_nav_menu (array (
...
'Theme_location' => 'primary',
...
)
);
菜单通过管理面板放置在页面上,该面板还允许其他页面将菜单(外观相同)放置在同一位置。
Custom settings can be made through parameters wp_nav_menu()
function, but should you need to create a custom menu, do this:
- Create inheritance class from
Walker_Nav_Menu
, where you can write your custom functionality: class Custom_Walker_Nav_Menu extends Walker_Nav_Menu {...}
.
- 显示菜单时,添加另一个参数walker:
if( has_nav_menu( 'primary' ) )
wp_nav_menu( array(
...
'theme_location' => 'primary',
'walker' => new Custom_Walker_Nav_Menu
...
)
);
问:请解释什么是循环以及它是如何工作的。
Loop means “cycle” from which WordPress的 generates the page content. If there is a single page, for example, page.php
or single.php
, the cycle remains with the template, but it will be realized only once. If you have search.php
, archive.php
or the index.php
, the cycle will take place as many times as necessary to satisfy the request, which depends on the query URL, and consequently, on how the object is formed wp_query
. Loop runs for posts in the global $wp_query
object.
Q: Explain the difference between functions that begin with the_
and other functions.
Functions starting with the_
are intended to be used in the loop. Note they can’t be used out 的 the cycle.
问:说明自定义帖子类型;我们为什么要使用它们,以及如何创建它们?
需要自定义帖子类型才能在逻辑上中断内容。例如,我们要列出公司中的所有销售经理,以便客户可以直接从我们的网站与他们联系。假设我们的销售部门大约有20人,而且在任何时候,其中一个人可能会辞职或新人加入。因此,对于此任务,最合乎逻辑的操作是创建帖子类型 球队 在模板列表中,每个帖子类型的人 球队.
创建自定义帖子类型的简单示例:
add_action( 'init', 'theme_create_post_type' );
function theme_create_post_type()
{
register_post_type( 'team',
array(
'labels' => array(
'name' => __( 'Team' ),
'singular_name' => __( 'team' ),
),
'publicly_queryable' => true,
'public' => true,
'show_ui' => true,
'hierarchical' => false,
'menu_position' => null,
'query_var' => true,
'supports' => array('title','editor','thumbnail','custom-fields','comments')
)
);
}
Custom post types are created at the 行动 init
. Therefore, you need to create 行动 and name it register_post_type()
in function with the desired set 的 parameters.
问:说明如何从自定义帖子类型和自定义分类中获取帖子。
例如,我们有一个帖子类型 球队 和分类法 部门,在自定义分类法中,我们有 管理人员。我们的任务是展示所有经理。
提供术语的“弹头” 管理人员 在习惯分类法中 部门 等于 管理人员,我们可以利用 WP_Query类, 像这样:
$args = array(
'post_type' => 'team',
'tax_query' => array(
array(
'taxonomy' => 'department',
'field' => 'slug',
'terms' => 'managers',
),
),
);
$query = new WP_Query( $args );
问:说明简码,并提供一些示例。
Shortcodes are keywords used to create macros that can be used later in a post’s content. In WordPress的, they are enclosed in square brackets []
. Shortcodes can be used either in the content editor’s post or in a pattern, if you apply do_shortcode()
function. The simplest example is when a client does not know HTML, but it is necessary to insert something in the content 的 the posts.
In WordPress的, we can create three types 的 shortcodes. However, because they can be configured in different ways, shortcodes have unlimited possibilities. It is important to note that the shortcode processing function must return the value, not display it. For example, if we need to display the phrase “Hello World”, we would write in functions.php
something like the following:
function hworld_func( $atts ){
return ‘<div class=”block-wrapper”><div class=”block”>Hello world</div></div>’;
}
add_shortcode( 'hworld', 'hworld_func' );
To get the shortcode to work, we need to insert [hworld]
into the post content.
If we have to adjust the shortcode from the admin panel, we can add parameters to it, like this: [hworld cssclass = "red"]
.
A code in the functions.php
will be as follows:
function hworld_func( $atts ){
return ‘<div class=”block-wrapper ’ . $atts[‘cssclass’] . ’”><div class=”block”>Hello world</div></div>’;
}
add_shortcode( 'hworld', 'hworld_func' );
Also, as well as the parameters, we can handle the text itself. To do that, the shortcode should look like this: [hworld cssclass = "red"] Hello world [/ hworld]
.
The functions.php
will then look like this:
function hworld_func( $atts, $content ){
return ‘<div class=”block-wrapper ’ . $atts[‘cssclass’] . ’”><div class=”block”>’ . $content . ’</div></div>’;
}
add_shortcode( 'hworld', 'hworld_func' );
问:描述如何制作小部件。解释主要的窗口小部件功能。
In WordPress的, you can create custom widgets 通过 creating a class that inherits basic functions from WP_Widget
class.
小部件类的主要功能:
__construct()
:小部件的构造函数,它将在管理面板中添加小部件。
widget($ args, $ instance)
: Displays the content on the front-end based on the function arguments. $ args
are widget arguments which are set in the sidebar to wrap a widget. $ instance
are parameters 的 the widget, which can be configured in the admin area.
update($ new_instance, $ old_instance)
:一种功能,如果管理员已在管理面板中更改了新的小部件选项,则该功能将保存。
form($ instance)
: Creates a widget settings form in the admin panel on the page widgets.php
.
The final thing to make the widget work is on the 行动 widgets_init
attach function that will record (register) it:
add_action ( 'widgets_init', create_function ( '', 'return register_widget ( "CustomWidget");'));
小部件示例:
class CustomWidget extends WP_Widget {
function __construct() { ... }
function widget( $args, $instance ) { ... }
function update( $new_instance, $old_instance ) { ... }
function form( $instance ) { ... }
}
add_action( 'widgets_init', create_function( '', 'return register_widget( "CustomWidget" );' ) );
问:请说明什么是动作和过滤器。讨论它们之间的差异,以及如何以及何时使用它们。
您无法更改WordPress核心文件和插件。但是,有时我们需要更改默认的核心行为,这是在采取操作和过滤器时。
它们的工作原理几乎相同,唯一的区别是强加给过滤器的函数返回值,而动作则不。因此,过滤器会更改某些内容或含义,稍后将由内核或插件使用。
Here’s an example 的 a filter that must be written in the functions.php
:
add_filter(‘the_content’, ‘theme_the_content’);
function theme_the_content($content){
return ‘<div class=”contentwrap”>’ . $content . ‘</div>’;
}
这意味着在核心代码或插件中的某处类似于以下内容的一行:
$content = apply_filters(‘the_content’, $content);
So, the variable $content
is the second parameter that’s sent to theme_the_content()
function. It may also look like the following code:
$var = apply_filters(‘some_filter_name’, $var1, $var2, $var2);
In this case, you need to write in functions.php
:
add_filter(‘some_filter_name’, ‘theme_some_filter_name’, 10, 3);
function theme_the_content($var1, $var2, $var2){
return [SOME_CODE];
}
In the filter
function, number 10 can be any integer that indicates the priority 的 this filter, if such filters will be several, and number 3 is a number 的 variables that come into function.
add_action( 'init', 'theme_create_post_type' );
function theme_create_post_type()
{
register_post_type( 'team',
array(
'labels' => array(
'name' => __( 'Team' ),
'singular_name' => __( 'team' ),
),
'publicly_queryable' => true,
'public' => true,
'show_ui' => true,
'hierarchical' => false,
'menu_position' => null,
'query_var' => true,
'supports' => array('title','editor','thumbnail','custom-fields','comments')
)
);
}
参数可以传递给动作和过滤器。如果我们不需要过滤器或操作,则可以将其删除。例如:
remove_filter ( 'some_filter_name', 'theme_some_filter_name', 10);
remove_action ( 'init', 'theme_create_post_type');
问:讨论将插件集成到主题的不同方法。说明正确的做法。
插件可以通过多种方式集成:
- 通过动作和过滤器。
- 通过简码。
- 通过小部件。
- 通过一个PHP函数。
如果插件是借助PHP代码集成的,则应检查是否存在函数:
<?php if(function_exists('bcn_display')) bcn_display(); ?>
It is important to check the theme’s ability to work without plugins, so that when the plugin is deactivated the theme will keep working. For example, if we use the plugin WP_PageNavi
, we should write code like this:
if(function_exists('wp_pagenavi')):
wp_pagenavi();
else:
get_template_part( 'blocks/pager' );
endif;
这意味着当您关闭插件时,主题将在保持功能页面的同时继续运行。但是,这将是一个标准页面,而不是随插件一起显示的页面。
问:描述创建一个简单插件的步骤。
The plugin is an addition; it is an expansion 的 WordPress的’s functionality. To create a plugin, you first need to create a folder in the wp-content/plugins
directory, using lower case, Roman alphabet, and no spaces. Inside this folder, you need to create a file with the same name as the folder using the extension .php
. This will be the main file. Additionally, create an empty index.php
file to hide the list 的 files and catalogs.
在主文件的开头,添加预定义的元数据行,WordPress可通过这些行了解其是插件:
<?php
/*
Plugin Name: [PLUGIN NAME]
Description: [PLUGIN NAME DESCRIPTION]
Version: [PLUGIN VERSION]
Author: [PLUGIN AUTHOR]
Author URI: [PLUGIN URL]
License: GPL2
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
建议使用类设计(创建)插件。因此,插件代码结构的示例可能是这样的:
<?php
/*
Plugin Name: TestPlugin_Name
Description: TestPlugin_Name description.
Version: 1.0.0
Author: [email protected]
Author URI: //www.linkedin.com/in/evgengavrilov
License: GPL2
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
class TestPlugin {
[CODE IS HERE]
function TestPlugin(){
[CODE IS HERE]
}
[CODE IS HERE]
}
$testplugin = new TestPlugin();
Remember the rules 的 creating a theme: Media may only connect through 行动 wp_enqueue_scripts
, and the plugin must not contain any errors.
问:请说明儿童主题的作用。您如何制作基本的儿童主题?
当您需要更改主主题或父主题的样式和功能而无需更改其代码时,则需要子主题。就像面向对象的编程一样:一个类及其派生类。继承不会破坏父级,但可以更改现有方法并添加新方法。
可以通过几个步骤快速轻松地创建一个子主题:
- Create a folder inside the
wp-content/themes
with the name [name-of-parents-folders-theme]-child
.
- Inside the folder, create
styles.css
file and to the main header file, add another line: Template: [name-of-parents-folders-theme]
.
- Create a
functions.php
file.
因此,我们可以通过添加到子文件夹模板文件来更改父主题的样式。 WordPress的将使用这些文件,并在必要时从父主题文件夹中使用其他文件。
问:解释一个WordPress网络,以及如何创建它。
WordPress的网络是通过单个管理面板管理多个站点的一种方式。管理员和超级管理员角色的主要区别在于,超级管理员可以管理网络(多站点)。
该网络可以在任何WordPress网站上建立。尽管此功能已集成,但默认情况下未激活。
创建网络:
- At the end 的 the
wp-config.php
file, write a line: define ( 'WP_ALLOW_MULTISITE', true);
- 刷新管理面板,“网络设置”子项应出现在“工具”菜单中。单击它,然后转到安装页面。填写WordPress创建的字段。
- Next, you will be transferred to the page settings that should be copied to
wp-config.php
and .htaccess
files. Simply copy the suggested code, and paste it into the appropriate file.
- 更改文件后,请记住退出管理控制台,以允许用户创建和管理网络的网站。
结论
我们希望您在少数精英WordPress程序员中搜索优秀的全职或兼职候选人时,发现本文中的问题和答案是有用的基础。寻找精通插件开发或主题开发的候选人是值得的,因为它们无疑将对您团队的生产力产生重大而积极的影响,并推动您的WordPress项目。