• ><\/span>\n<\/span>\n ><\/span>\n<\/span> ><\/span><\/span><\/h1<\/span>><\/span>\n<\/span> ><\/span><\/span><\/h3<\/span>><\/span>\n<\/span> <\/header<\/span>><\/span>\n<\/span>\n <\/span> if ( have_posts() ) :\n<\/span><\/span> \/* Start the Loop *\/\n<\/span><\/span> while ( have_posts() ) :\n<\/span><\/span> the_post();\n<\/span><\/span> endwhile;\n<\/span><\/span> endif;\n<\/span><\/span> ?><\/span>\n<\/span>\n<\/body<\/span>><\/span>\n<\/span><\/pre>\n

    We need to fill that while loop with content — or with content-outputting WordPress tags.<\/p>\n

    If we change our loop, by adding the_title(), the_excerpt(), and we add HTML markup and the_ID(), to look like this:<\/p>\n

    <\/span>if ( have_posts() ) {\n<\/span><\/span>    while ( have_posts() ) {\n<\/span><\/span>        the_post();\n<\/span><\/span>        \/\/\n<\/span><\/span>        \/\/ Post Content here\n<\/span><\/span>        \/\/\n<\/span><\/span>    } \/\/ end while\n<\/span><\/span>} \/\/ end if\n<\/span><\/span>?><\/span>\n<\/span><\/pre>\n

    We’ll now get a list of posts on our home page, with no style applied:<\/p>\n\n

    \"How<\/p>\n

    WordPress shows A blog page — an archive page for all the blog posts — by default.<\/p>\n

    If we now visit single post URL — something like http:\/\/my-website.com\/2018\/11\/14\/sapiente-ad-facilis-quo-repellat-quos\/ — we’ll see something like this:<\/p>\n

    \"How<\/p>\n

    Our loop<\/em>, albeit very crude, actually works.<\/p>\n

    Structuring Our Theme into Files and Applying Bootstrap Markup<\/h2>\n

    We’ll now implement partials, like header.php and footer.php and various specialized templates, all using Twitter Bootstrap markup, so that we can style it more easily.<\/p>\n

    Starting with index.php, we replace all the content before and after the loop with get_header() and get_footer() functions:<\/p>\n

        <\/span>    if ( have_posts() ) : while ( have_posts() ): the_post(); ?><\/span>\n<\/span>\n     id=\"post-<\/span>\"<\/span>><\/span>\n<\/span>        ><\/span><\/span><\/h2<\/span>><\/span>\n<\/span>         class=\"post-excerpt\"<\/span>><\/span><\/span><\/div<\/span>><\/span>\n<\/span>    <\/div<\/span>><\/span>\n<\/span>\n    <\/span>    endif;\n<\/span><\/span>    ?><\/span>\n<\/span><\/pre>\n

    This means we need to provide all that content in the partials we mentioned.<\/p>\n

    In line with what we said — that we’ll use Twitter Bootstrap theme — our header.php file will look like this:<\/p>\n

    <\/span>\/**\n<\/span><\/span> *\n<\/span><\/span> * @package Botega_Scratch_Theme\n<\/span><\/span><\/span> *\/\n<\/span><\/span>\n<\/span>get_header(); ?><\/span>\n<\/span>\n    <\/span>    if ( have_posts() ) : while ( have_posts() ): the_post(); ?><\/span>\n<\/span>\n     id=\"post-<\/span>\"<\/span>><\/span>\n<\/span>        ><\/span><\/span><\/h2<\/span>><\/span>\n<\/span>         class=\"post-excerpt\"<\/span>><\/span><\/span><\/div<\/span>><\/span>\n<\/span>    <\/div<\/span>><\/span>\n<\/span>\n    <\/span>    endif;\n<\/span><\/span>    ?><\/span>\n<\/span>\n<\/span>\n<\/span><\/pre>\n

    Our footer.php file will look like this:<\/p>\n

    \/*\n<\/span>Theme Name: Botega Simple Theme\n<\/span>Theme URI: https:\/\/botega.co.uk\n<\/span>Author: Tonino Jankov\n<\/span>Author URI: https:\/\/botega.co.uk\n<\/span>Description: Basic WordPress theme for Sitepoint theme building tutorial\n<\/span>Text Domain: bsimple\n<\/span>Version: 1.0.0\n<\/span>License: GNU General Public License v2 or later\n<\/span>*\/\n<\/span><\/pre>\n

    We are using Bootstrap classes in our HTML tags, and wp_head() and wp_footer() fire wp_head and wp_footer action hooks.<\/p>\n

    The next thing we’ll do is include the CSS and JavaScript from clean bootstrap template from startbootstrap.com, which comes with an MIT license, so we can freely use it. This way, our theme will come with predefined styles, responsiveness, and we’ll still be able to style it further.<\/p>\n

    functions.php<\/h2>\n

    functions.php is a file that comes with any serious WordPress theme. This is a file that acts as a poor man’s plugin archive. It allows us to include any custom functionality in our theme.<\/p>\n

    We’ll firstly use this file to include Bootstrap and our bootstrap theme’s styles and scripts:<\/p>\n

    <\/span>\/**\n<\/span><\/span> *\n<\/span><\/span> * @package Botega_Scratch_Theme\n<\/span><\/span><\/span> *\/\n<\/span><\/span>?><\/span>\n<\/span>\n<\/span>\n<\/span> <\/span><\/span>><\/span>\n<\/span>><\/span>\n<\/span>    ><\/span><\/span><\/title<\/span>><\/span>\n<\/span>     rel=\"stylesheet\"<\/span> href=\"<\/span>\"<\/span>><\/span>\n<\/span>    <\/span>\n<\/span><\/head<\/span>><\/span>\n<\/span>
    

    亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

    ><\/span>\n<\/span>\n ><\/span>\n<\/span> ><\/span><\/span><\/h1<\/span>><\/span>\n<\/span> ><\/span><\/span><\/h3<\/span>><\/span>\n<\/span> <\/header<\/span>><\/span>\n<\/span>\n <\/span> if ( have_posts() ) :\n<\/span><\/span> \/* Start the Loop *\/\n<\/span><\/span> while ( have_posts() ) :\n<\/span><\/span> the_post();\n<\/span><\/span> endwhile;\n<\/span><\/span> endif;\n<\/span><\/span> ?><\/span>\n<\/span>\n<\/body<\/span>><\/span>\n<\/span><\/pre>\n

    This is a WordPress-idiomatic way of including scripts and styles in a theme. It allows us to specify that the position of the scripts will be enqueued (header vs footer) and the priority of enqueuing. We can even specify the dependency of each particular resource on the other. This will ensure resources will be loaded in the right order.<\/p>\n

    We’re using the wp_enqueue_scripts action hook here. We can learn more about it in the Codex. (We covered action hooks in the previous article.)<\/p>\n

    Inside our custom bsimple_scripts() function — which we hook to wp_enqueue_scripts action hook — we use two WordPress functions to load our scripts and styles — wp_enqueue_script() and wp_enqueue_style(). Arguments for these functions — as specified in its linked reference pages — allow us to fully leverage flexibility that we mentioned.<\/p>\n

    We can see that we’re loading styles from the Internet (Google fonts) and from our theme folder. Therefore, we create css, js and webfonts directories in our theme folder, and copy our Bootstrap theme’s CSS, JavaScript files, and FontAwesome icon-font files.<\/p>\n

    We also copy our index.php file to archive.php, page.php and single.php files, which we’ll modify.<\/p>\n

    Now our theme file structure will look something like this:<\/p>\n

    \"How<\/p>\n

    Adjusting the Markup<\/h2>\n

    If we now visit our home page, we’ll see the menu on the top — though it and the page are still a mess – because the following line in our header is still outputting the menu wrapped in div and its own ul tags, so it isn’t affected by our bootstrap styles:<\/p>\n

    <\/span>if ( have_posts() ) {\n<\/span><\/span>    while ( have_posts() ) {\n<\/span><\/span>        the_post();\n<\/span><\/span>        \/\/\n<\/span><\/span>        \/\/ Post Content here\n<\/span><\/span>        \/\/\n<\/span><\/span>    } \/\/ end while\n<\/span><\/span>} \/\/ end if\n<\/span><\/span>?><\/span>\n<\/span><\/pre>\n

    To solve this, we first need to go to our wp-admin dashboard and create — in the customizer — a new menu. we’ll name it Top Menu<\/em>.<\/p>\n

    After we’ve done this, we’ll go to our header.php file remove these lines:<\/p>\n

    \/*\n<\/span>Theme Name: Botega Simple Theme\n<\/span>Theme URI: https:\/\/botega.co.uk\n<\/span>Author: Tonino Jankov\n<\/span>Author URI: https:\/\/botega.co.uk\n<\/span>Description: Basic WordPress theme for Sitepoint theme building tutorial\n<\/span>Text Domain: bsimple\n<\/span>Version: 1.0.0\n<\/span>License: GNU General Public License v2 or later\n<\/span>*\/\n<\/span><\/pre>\n

    In their place we put these lines:<\/p>\n

    <\/span>\/**\n<\/span><\/span> *\n<\/span><\/span> * @package Botega_Scratch_Theme\n<\/span><\/span><\/span> *\/\n<\/span><\/span>?><\/span>\n<\/span>\n<\/span>\n<\/span> <\/span><\/span>><\/span>\n<\/span>><\/span>\n<\/span>    ><\/span><\/span><\/title<\/span>><\/span>\n<\/span>     rel=\"stylesheet\"<\/span> href=\"<\/span>\"<\/span>><\/span>\n<\/span>    <\/span>\n<\/span><\/head<\/span>><\/span>\n<\/span>><\/span>\n<\/span>\n      ><\/span>\n<\/span>         ><\/span><\/span><\/h1<\/span>><\/span>\n<\/span>         ><\/span><\/span><\/h3<\/span>><\/span>\n<\/span>      <\/header<\/span>><\/span>\n<\/span>\n        <\/span>        if ( have_posts() ) :\n<\/span><\/span>            \/* Start the Loop *\/\n<\/span><\/span>            while ( have_posts() ) :\n<\/span><\/span>                the_post();\n<\/span><\/span>            endwhile;\n<\/span><\/span>        endif;\n<\/span><\/span>        ?><\/span>\n<\/span>\n<\/body<\/span>><\/span>\n<\/span><\/pre>\n

    This will remove the div tag and the duplication of the ul tag for us, but we still need to apply nav-item and nav-link to our menu items (to li and a tags respectively). How will we go about this? wp_nav_menu does not provide arguments for this. We’ll use the nav_menu_link_attributes and nav_menu_css_class filter hooks. We put this into our functions.php file:<\/p>\n

    <\/span>if ( have_posts() ) {\n<\/span><\/span>    while ( have_posts() ) {\n<\/span><\/span>        the_post();\n<\/span><\/span>        \/\/\n<\/span><\/span>        \/\/ Post Content here\n<\/span><\/span>        \/\/\n<\/span><\/span>    } \/\/ end while\n<\/span><\/span>} \/\/ end if\n<\/span><\/span>?><\/span>\n<\/span><\/pre>\n

    Now we can specify new attributes in our wp_nav_menu in our header.php:<\/p>\n

        <\/span>    if ( have_posts() ) : while ( have_posts() ): the_post(); ?><\/span>\n<\/span>\n     id=\"post-<\/span>\"<\/span>><\/span>\n<\/span>        ><\/span><\/span><\/h2<\/span>><\/span>\n<\/span>         class=\"post-excerpt\"<\/span>><\/span><\/span><\/div<\/span>><\/span>\n<\/span>    <\/div<\/span>><\/span>\n<\/span>\n    <\/span>    endif;\n<\/span><\/span>    ?><\/span>\n<\/span><\/pre>\n

    Now our top menu links can take advantage of styles already defined in our Bootstrap theme’s CSS.<\/p>\n

    Dynamic Header<\/h2>\n

    To be able to use a dynamic header — that is, a different header for the front page, for other selected pages, or for archives — we’ll define a dynamic_header() function in our functions.php file, where we’ll output our header markup dependent on the page the visitor loads.<\/p>\n

    So now our header.php file will end like this:<\/p>\n

    <\/span>\/**\n<\/span><\/span> *\n<\/span><\/span> * @package Botega_Scratch_Theme\n<\/span><\/span><\/span> *\/\n<\/span><\/span>\n<\/span>get_header(); ?><\/span>\n<\/span>\n    <\/span>    if ( have_posts() ) : while ( have_posts() ): the_post(); ?><\/span>\n<\/span>\n     id=\"post-<\/span>\"<\/span>><\/span>\n<\/span>        ><\/span><\/span><\/h2<\/span>><\/span>\n<\/span>         class=\"post-excerpt\"<\/span>><\/span><\/span><\/div<\/span>><\/span>\n<\/span>    <\/div<\/span>><\/span>\n<\/span>\n    <\/span>    endif;\n<\/span><\/span>    ?><\/span>\n<\/span>\n<\/span>\n<\/span><\/pre>\n

    We’ll also define that function like this:<\/p>\n

    <\/span>\/**\n<\/span><\/span> * The header for our theme.\n<\/span><\/span> *\n<\/span><\/span> * @package Botega_Scratch_Theme\n<\/span><\/span><\/span> *\n<\/span><\/span> *\/\n<\/span><\/span>?><\/span>\n<\/span><\/span>\n<\/span>><\/span>\n<\/span>><\/span>\n<\/span> charset=\"<\/span>\"<\/span>><\/span>\n<\/span> name=\"viewport\"<\/span> content=\"width=device-width, initial-scale=1\"<\/span>><\/span>\n<\/span>\n<\/span>\n<\/span><\/head<\/span>><\/span>\n<\/span>\n <\/span><\/span>><\/span>\n<\/span>\n   class=\"navbar navbar-default navbar-custom navbar-fixed-top\"<\/span>><\/span>\n<\/span>     class=\"container-fluid\"<\/span>><\/span>\n<\/span>       class=\"navbar-header page-scroll\"<\/span>><\/span>\n<\/span>               href=\"<\/span>\"<\/span> rel=\"home\"<\/span> class=\"navbar-brand\"<\/span>><\/span><\/span><\/a<\/span>><\/span>\n<\/span>      <\/div<\/span>><\/span>\n<\/span>\n       class=\"collapse navbar-collapse\"<\/span> id=\"bs-example-navbar-collapse-1\"<\/span>><\/span>\n<\/span>           class=\"nav navbar-nav navbar-right\"<\/span>><\/span>\n<\/span>           'primary', 'items_wrap' => '%3$s' ) ); ?><\/span>\n<\/span>          <\/ul<\/span>><\/span>\n<\/span>      <\/div<\/span>><\/span>\n<\/span>    <\/div<\/span>><\/span>\n<\/span>  <\/nav<\/span>><\/span>\n<\/span>\n   class=\"container\"<\/span>><\/span>\n<\/span>       class=\"row\"<\/span>><\/span>\n<\/span><\/pre>\n

    To be able to use all the current URL or post data — like in the loop — we declare a $post variable global. Then we just fill different page or request cases with filler header HTML, which we’ll finish later. This sets the foundation for a truly dynamic header.<\/p>\n

    We need to make sure that our front page — with dynamic top menu — will look good even when the user is logged in. WordPress shows an admin bar<\/em> when visitors are logged in, even when they visit the front page. Because it has position: fixed, it overlays the top zone on our website, covering whatever is there, so we need to specify an offset for our top menu.<\/p>\n

    We’ll add this to our style.css:<\/p>\n

    <\/span>\/**\n<\/span><\/span> * Footer template partial\n<\/span><\/span> *\n<\/span><\/span> * @package Botega_Scratch_Theme\n<\/span><\/span><\/span> *\n<\/span><\/span> *\/\n<\/span><\/span>?><\/span>\n<\/span>     <\/div<\/span>><\/span>\n<\/span>     \n<\/span>  <\/div<\/span>><\/span>\n<\/span>  \n<\/span>\n  \n<\/span>  ><\/span>\n<\/span>     class=\"container\"<\/span>><\/span>\n<\/span>       class=\"row\"<\/span>><\/span>\n<\/span>           class=\"col-lg-8 col-md-10 mx-auto\"<\/span>><\/span>\n<\/span>          <\/div<\/span>><\/span>\n<\/span>      <\/div<\/span>><\/span>\n<\/span>    <\/div<\/span>><\/span>\n<\/span>  <\/footer<\/span>><\/span>\n<\/span>\n<\/span>\n<\/span>\n<\/body<\/span>><\/span>\n<\/span><\/html<\/span>><\/span>\n<\/span><\/pre>\n

    This makes sure the #mainNav — our menu container — has enough offset from the top, so it isn’t covered when user is logged in. WordPress adds logged-in and admin-bar classes to body in these cases, so we can easily target it.<\/p>\n

    We can see that we address two cases in our CSS — one default, and another one for smaller screens. This is because WordPress outputs a wider admin bar on mobile devices, so we need to provide a 46px offset.<\/p>\n

    On mobile, we should now have a responsive, JavaScript-powered dropdown menu:<\/p>\n

    \"How<\/p>\n

    Conclusion<\/h2>\n

    In this second part on creating a WordPress theme from scratch, we created a very basic WordPress theme, and we included Bootstrap styles and scripts into it. We adjusted the menu output to fit our predefined styles. We also separated header and footer output into their respective partials.<\/p>\n

    The functions.php file — a crucial file in theme development — is another topic we introduced and leveraged. Header output has been separated into its own function, which will use particulars of page visit, and site-owner–defined variables to determine the final output.<\/p>\n

    In the third part of the guide, we’ll finish building particular templates, give better structure to our theme functions and partials, and finish up the styling of our website.<\/p>\n


    \n

    There are three articles in this series on building a WordPress theme from scratch:<\/p>\n

      \n
    • understanging a theme’s structure<\/li>\n
    • theme basics<\/li>\n
    • refining the theme<\/li>\n<\/ul>\n\n\n\n

      Frequently Asked Questions (FAQs) about Building a WordPress Theme from Scratch<\/h2>\n\n\n\n

      What are the prerequisites for building a WordPress theme from scratch?<\/h3>

      Before you start building a WordPress theme from scratch, you need to have a basic understanding of HTML, CSS, PHP, and JavaScript. These are the core technologies used in WordPress theme development. Additionally, you should be familiar with the WordPress platform itself, including its file structure and template hierarchy. It’s also helpful to have a local development environment set up on your computer, such as MAMP or XAMPP, where you can test your theme as you build it.<\/p>

      How do I start building a WordPress theme from scratch?<\/h3>

      The first step in building a WordPress theme from scratch is to create a new directory in your WordPress themes folder. This will be the home for all your theme files. Next, you’ll need to create a style.css file and an index.php file. The style.css file is where you’ll write all your CSS code, and it’s also where you’ll define your theme details. The index.php file is the main template file for your theme. It’s where you’ll write the PHP and HTML code that generates your website’s layout.<\/p>

      How can I add custom functionality to my WordPress theme?<\/h3>

      You can add custom functionality to your WordPress theme by creating a functions.php file in your theme directory. This file acts like a plugin, allowing you to add custom features and functionality to your theme. You can use it to register navigation menus, add sidebars, enqueue styles and scripts, and much more.<\/p>

      How do I create a responsive WordPress theme?<\/h3>

      To create a responsive WordPress theme, you’ll need to use media queries in your CSS code. Media queries allow you to apply different styles depending on the size of the user’s screen. This means you can create a different layout for desktop, tablet, and mobile devices. You’ll also need to make sure your images are responsive, which you can do by setting their width to 100%.<\/p>\n

      How do I customize the header and footer of my WordPress theme?<\/h3>

      You can customize the header and footer of your WordPress theme by creating a header.php file and a footer.php file in your theme directory. The header.php file is where you’ll write the HTML and PHP code for your header, and the footer.php file is where you’ll write the code for your footer. You can then include these files in your other template files using the get_header() and get_footer() functions.<\/p>

      How do I add a custom post type to my WordPress theme?<\/h3>

      You can add a custom post type to your WordPress theme by using the register_post_type() function in your functions.php file. This function allows you to define a new post type with its own labels, capabilities, and features. You can then create a single-{posttype}.php file and an archive-{posttype}.php file to control the display of your custom post type.<\/p>

      How do I add a sidebar to my WordPress theme?<\/h3>

      You can add a sidebar to your WordPress theme by creating a sidebar.php file in your theme directory and using the register_sidebar() function in your functions.php file. You can then display your sidebar in your other template files using the get_sidebar() function.<\/p>

      How do I add a navigation menu to my WordPress theme?<\/h3>

      You can add a navigation menu to your WordPress theme by using the register_nav_menus() function in your functions.php file. This function allows you to register one or more navigation menus in your theme. You can then display your menu in your other template files using the wp_nav_menu() function.<\/p>

      How do I customize the loop in my WordPress theme?<\/h3>

      You can customize the loop in your WordPress theme by modifying the loop code in your index.php file or other template files. The loop is the PHP code that WordPress uses to display posts. You can customize it to change the way posts are displayed, the number of posts displayed, and more.<\/p>

      How do I update my WordPress theme?<\/h3>

      You can update your WordPress theme by making changes to your theme files and then uploading them to your WordPress site. If you’re using a child theme, you can update the parent theme without losing your changes. If you’re not using a child theme, you should make a backup of your theme before updating it, as updates will overwrite your changes.<\/p>"}

      Table of Contents
      Key Takeaways
      Creating the Bare Minimum Theme
      Structuring Our Theme into Files and Applying Bootstrap Markup
      functions.php
      Adjusting the Markup
      Dynamic Header
      Conclusion
      Frequently Asked Questions (FAQs) about Building a WordPress Theme from Scratch
      What are the prerequisites for building a WordPress theme from scratch?
      How do I start building a WordPress theme from scratch?
      How can I add custom functionality to my WordPress theme?
      How do I create a responsive WordPress theme?
      How do I customize the header and footer of my WordPress theme?
      How do I add a custom post type to my WordPress theme?
      How do I add a sidebar to my WordPress theme?
      How do I add a navigation menu to my WordPress theme?
      How do I customize the loop in my WordPress theme?
      How do I update my WordPress theme?
      Home CMS Tutorial WordPress How to Build a WordPress Theme from Scratch: the Basics

      How to Build a WordPress Theme from Scratch: the Basics

      Feb 09, 2025 am 10:43 AM

      How to Build a WordPress Theme from Scratch: the Basics

      In this tutorial, we’ll explore WordPress theme file structure in depth, and learn how to create a basic WordPress theme from scratch.

      In the first part of this series, we introduced WordPress theming, and the fundamental terminology relating to WordPress theme development. We covered templates, partials, template hierarchy, WordPress post types, the style.css stylesheet, WordPress filter and action hooks, WordPress loop, conditional tags, and we briefly took a look at a typical simple WordPress theme file structure.

      Key Takeaways

      • Start by setting up a bare minimum theme using an index.php and style.css file, ensuring WordPress recognizes your new theme.
      • Integrate Bootstrap for responsive design and aesthetics, modifying template files to include Bootstrap’s HTML and CSS classes.
      • Utilize the functions.php file to add custom functionality, enqueue styles and scripts, and manage WordPress hooks effectively.
      • Customize the dynamic output of the header and navigation menus using WordPress hooks and conditionals to adapt to different page types.
      • Ensure your theme is responsive and user-friendly on mobile devices by adjusting CSS and using media queries.

      Creating the Bare Minimum Theme

      The first thing we’ll do is install a plugin that will enable us to batch create WordPress posts and other content. This way, we’ll be able to quickly populate our development website without losing too much time. One plugin that serves this purpose is FakerPress by Gustavo Bordoni, available in the WordPress plugin repository.

      We quickly install and activate the plugin via WP-CLI.

      Now, when we log in to the admin dashboard, we’ll see that FakerPress is installed, and we can create all sorts of content in batch, including any custom post types we have.

      How to Build a WordPress Theme from Scratch: the Basics

      Now, using this plugin, we’ll create some fake content. This is the result, using the default TwentySeventeen WordPress theme:

      How to Build a WordPress Theme from Scratch: the Basics

      Now, we quickly dive in and set up a bare minimum theme that consists of the catch-all index.php file, and style.css, which we need for the WordPress templating system to recognize the theme:

      <span>/*
      </span><span>Theme Name: Botega Simple Theme
      </span><span>Theme URI: https://botega.co.uk
      </span><span>Author: Tonino Jankov
      </span><span>Author URI: https://botega.co.uk
      </span><span>Description: Basic WordPress theme for Sitepoint theme building tutorial
      </span><span>Text Domain: bsimple
      </span><span>Version: 1.0.0
      </span><span>License: GNU General Public License v2 or later
      </span><span>*/
      </span>

      This is the style.css, which consists only of meta CSS comments for now. These comments are required.

      This is the index.php file. It will catch all the requests for now:

      <span>/*
      </span><span>Theme Name: Botega Simple Theme
      </span><span>Theme URI: https://botega.co.uk
      </span><span>Author: Tonino Jankov
      </span><span>Author URI: https://botega.co.uk
      </span><span>Description: Basic WordPress theme for Sitepoint theme building tutorial
      </span><span>Text Domain: bsimple
      </span><span>Version: 1.0.0
      </span><span>License: GNU General Public License v2 or later
      </span><span>*/
      </span>

      We now upload and activate the minimal theme we have. I activate it using WP-CLI:

      The theme is now visible to WordPress, and is active:

      How to Build a WordPress Theme from Scratch: the Basics

      We haven’t provided a screenshot, so the display in the backend is basic.

      If we visit our website now in the browser, this is what we’ll see:

      How to Build a WordPress Theme from Scratch: the Basics

      Obviously, we have work to do.

      If we view the source code of the home page, we’ll see that the wp_head() function has outputted a lot of default WordPress tags in the

      , like CSS, JavaScript, link and meta tags.

      The bloginfo() function is used to output website information.

      Our home page is empty, because we aren’t outputting anything inside the Loop — a pattern that WordPress uses in all of its templates to output content.

      The Codex page about the Loop goes deep into details about it. A typical structure for the loop — which is based on PHP while control structure — looks like this:

      <span><span><?php
      </span></span><span><span>/**
      </span></span><span><span> *
      </span></span><span><span> * <span>@package Botega_Scratch_Theme
      </span></span></span><span><span> */
      </span></span><span><span>?></span>
      </span>
      <span><span><!DOCTYPE html></span>
      </span><span><span><span><html</span> <span><span><?php language_attributes(); ?></span></span>></span>
      </span><span><span><span><head</span>></span>
      </span>    <span><span><span><title</span>></span><span><?php bloginfo('name'); ?></span><span><span></title</span>></span>
      </span>    <span><span><span><link</span> rel<span>="stylesheet"</span> href<span>="<span><?php bloginfo('stylesheet_url'); ?></span>"</span>></span>
      </span>    <span><span><?php wp_head(); ?></span>
      </span><span><span><span></head</span>></span>
      </span><span><span><span><body</span>></span>
      </span>
            <span><span><span><header</span>></span>
      </span>         <span><span><span><h1</span>></span><span><?php bloginfo('name'); ?></span><span><span></h1</span>></span>
      </span>         <span><span><span><h3</span>></span><span><?php bloginfo('description'); ?></span><span><span></h3</span>></span>
      </span>      <span><span><span></header</span>></span>
      </span>
              <span><span><?php
      </span></span><span>        <span>if ( have_posts() ) :
      </span></span><span>            <span>/* Start the Loop */
      </span></span><span>            <span>while ( have_posts() ) :
      </span></span><span>                <span>the_post();
      </span></span><span>            <span>endwhile;
      </span></span><span>        <span>endif;
      </span></span><span>        <span>?></span>
      </span>
      <span><span><span></body</span>></span>
      </span>

      We need to fill that while loop with content — or with content-outputting WordPress tags.

      If we change our loop, by adding the_title(), the_excerpt(), and we add HTML markup and the_ID(), to look like this:

      <span><span><?php
      </span></span><span><span>if ( have_posts() ) {
      </span></span><span>    <span>while ( have_posts() ) {
      </span></span><span>        <span>the_post();
      </span></span><span>        <span>//
      </span></span><span>        <span>// Post Content here
      </span></span><span>        <span>//
      </span></span><span>    <span>} // end while
      </span></span><span><span>} // end if
      </span></span><span><span>?></span>
      </span>

      We’ll now get a list of posts on our home page, with no style applied:

      How to Build a WordPress Theme from Scratch: the Basics

      WordPress shows A blog page — an archive page for all the blog posts — by default.

      If we now visit single post URL — something like http://my-website.com/2018/11/14/sapiente-ad-facilis-quo-repellat-quos/ — we’ll see something like this:

      How to Build a WordPress Theme from Scratch: the Basics

      Our loop, albeit very crude, actually works.

      Structuring Our Theme into Files and Applying Bootstrap Markup

      We’ll now implement partials, like header.php and footer.php and various specialized templates, all using Twitter Bootstrap markup, so that we can style it more easily.

      Starting with index.php, we replace all the content before and after the loop with get_header() and get_footer() functions:

          <span><span><?php
      </span></span><span>    <span>if ( have_posts() ) : while ( have_posts() ): the_post(); ?></span>
      </span>
          <span><span><span><div</span> id<span>="post-<span><?php the_ID(); ?></span>"</span>></span>
      </span>        <span><span><span><h2</span>></span><span><?php the_title(); ?></span><span><span></h2</span>></span>
      </span>        <span><span><span><div</span> class<span>="post-excerpt"</span>></span><span><?php the_excerpt(); ?></span><span><span></div</span>></span>
      </span>    <span><span><span></div</span>></span>
      </span>
          <span><span><?php endwhile;
      </span></span><span>    <span>endif;
      </span></span><span>    <span>?></span>
      </span>

      This means we need to provide all that content in the partials we mentioned.

      In line with what we said — that we’ll use Twitter Bootstrap theme — our header.php file will look like this:

      /**
       *
       * @package Botega_Scratch_Theme
       */
      
      get_header(); ?>
      
          <span><span><?php
      </span></span><span>    <span>if ( have_posts() ) : while ( have_posts() ): the_post(); ?></span>
      </span>
          <span><span><span><div</span> id<span>="post-<span><?php the_ID(); ?></span>"</span>></span>
      </span>        <span><span><span><h2</span>></span><span><?php the_title(); ?></span><span><span></h2</span>></span>
      </span>        <span><span><span><div</span> class<span>="post-excerpt"</span>></span><span><?php the_excerpt(); ?></span><span><span></div</span>></span>
      </span>    <span><span><span></div</span>></span>
      </span>
          <span><span><?php endwhile;
      </span></span><span>    <span>endif;
      </span></span><span>    <span>?></span>
      </span>
      
      

      Our footer.php file will look like this:

      <span>/*
      </span><span>Theme Name: Botega Simple Theme
      </span><span>Theme URI: https://botega.co.uk
      </span><span>Author: Tonino Jankov
      </span><span>Author URI: https://botega.co.uk
      </span><span>Description: Basic WordPress theme for Sitepoint theme building tutorial
      </span><span>Text Domain: bsimple
      </span><span>Version: 1.0.0
      </span><span>License: GNU General Public License v2 or later
      </span><span>*/
      </span>

      We are using Bootstrap classes in our HTML tags, and wp_head() and wp_footer() fire wp_head and wp_footer action hooks.

      The next thing we’ll do is include the CSS and JavaScript from clean bootstrap template from startbootstrap.com, which comes with an MIT license, so we can freely use it. This way, our theme will come with predefined styles, responsiveness, and we’ll still be able to style it further.

      functions.php

      functions.php is a file that comes with any serious WordPress theme. This is a file that acts as a poor man’s plugin archive. It allows us to include any custom functionality in our theme.

      We’ll firstly use this file to include Bootstrap and our bootstrap theme’s styles and scripts:

      <span><span><?php
      </span></span><span><span>/**
      </span></span><span><span> *
      </span></span><span><span> * <span>@package Botega_Scratch_Theme
      </span></span></span><span><span> */
      </span></span><span><span>?></span>
      </span>
      <span><span><!DOCTYPE html></span>
      </span><span><span><span><html</span> <span><span><?php language_attributes(); ?></span></span>></span>
      </span><span><span><span><head</span>></span>
      </span>    <span><span><span><title</span>></span><span><?php bloginfo('name'); ?></span><span><span></title</span>></span>
      </span>    <span><span><span><link</span> rel<span>="stylesheet"</span> href<span>="<span><?php bloginfo('stylesheet_url'); ?></span>"</span>></span>
      </span>    <span><span><?php wp_head(); ?></span>
      </span><span><span><span></head</span>></span>
      </span><span><span><span><body</span>></span>
      </span>
            <span><span><span><header</span>></span>
      </span>         <span><span><span><h1</span>></span><span><?php bloginfo('name'); ?></span><span><span></h1</span>></span>
      </span>         <span><span><span><h3</span>></span><span><?php bloginfo('description'); ?></span><span><span></h3</span>></span>
      </span>      <span><span><span></header</span>></span>
      </span>
              <span><span><?php
      </span></span><span>        <span>if ( have_posts() ) :
      </span></span><span>            <span>/* Start the Loop */
      </span></span><span>            <span>while ( have_posts() ) :
      </span></span><span>                <span>the_post();
      </span></span><span>            <span>endwhile;
      </span></span><span>        <span>endif;
      </span></span><span>        <span>?></span>
      </span>
      <span><span><span></body</span>></span>
      </span>

      This is a WordPress-idiomatic way of including scripts and styles in a theme. It allows us to specify that the position of the scripts will be enqueued (header vs footer) and the priority of enqueuing. We can even specify the dependency of each particular resource on the other. This will ensure resources will be loaded in the right order.

      We’re using the wp_enqueue_scripts action hook here. We can learn more about it in the Codex. (We covered action hooks in the previous article.)

      Inside our custom bsimple_scripts() function — which we hook to wp_enqueue_scripts action hook — we use two WordPress functions to load our scripts and styles — wp_enqueue_script() and wp_enqueue_style(). Arguments for these functions — as specified in its linked reference pages — allow us to fully leverage flexibility that we mentioned.

      We can see that we’re loading styles from the Internet (Google fonts) and from our theme folder. Therefore, we create css, js and webfonts directories in our theme folder, and copy our Bootstrap theme’s CSS, JavaScript files, and FontAwesome icon-font files.

      We also copy our index.php file to archive.php, page.php and single.php files, which we’ll modify.

      Now our theme file structure will look something like this:

      How to Build a WordPress Theme from Scratch: the Basics

      Adjusting the Markup

      If we now visit our home page, we’ll see the menu on the top — though it and the page are still a mess – because the following line in our header is still outputting the menu wrapped in div and its own ul tags, so it isn’t affected by our bootstrap styles:

      <span><span><?php
      </span></span><span><span>if ( have_posts() ) {
      </span></span><span>    <span>while ( have_posts() ) {
      </span></span><span>        <span>the_post();
      </span></span><span>        <span>//
      </span></span><span>        <span>// Post Content here
      </span></span><span>        <span>//
      </span></span><span>    <span>} // end while
      </span></span><span><span>} // end if
      </span></span><span><span>?></span>
      </span>

      To solve this, we first need to go to our wp-admin dashboard and create — in the customizer — a new menu. we’ll name it Top Menu.

      After we’ve done this, we’ll go to our header.php file remove these lines:

      <span>/*
      </span><span>Theme Name: Botega Simple Theme
      </span><span>Theme URI: https://botega.co.uk
      </span><span>Author: Tonino Jankov
      </span><span>Author URI: https://botega.co.uk
      </span><span>Description: Basic WordPress theme for Sitepoint theme building tutorial
      </span><span>Text Domain: bsimple
      </span><span>Version: 1.0.0
      </span><span>License: GNU General Public License v2 or later
      </span><span>*/
      </span>

      In their place we put these lines:

      <span><span><?php
      </span></span><span><span>/**
      </span></span><span><span> *
      </span></span><span><span> * <span>@package Botega_Scratch_Theme
      </span></span></span><span><span> */
      </span></span><span><span>?></span>
      </span>
      <span><span><!DOCTYPE html></span>
      </span><span><span><span><html</span> <span><span><?php language_attributes(); ?></span></span>></span>
      </span><span><span><span><head</span>></span>
      </span>    <span><span><span><title</span>></span><span><?php bloginfo('name'); ?></span><span><span></title</span>></span>
      </span>    <span><span><span><link</span> rel<span>="stylesheet"</span> href<span>="<span><?php bloginfo('stylesheet_url'); ?></span>"</span>></span>
      </span>    <span><span><?php wp_head(); ?></span>
      </span><span><span><span></head</span>></span>
      </span><span><span><span><body</span>></span>
      </span>
            <span><span><span><header</span>></span>
      </span>         <span><span><span><h1</span>></span><span><?php bloginfo('name'); ?></span><span><span></h1</span>></span>
      </span>         <span><span><span><h3</span>></span><span><?php bloginfo('description'); ?></span><span><span></h3</span>></span>
      </span>      <span><span><span></header</span>></span>
      </span>
              <span><span><?php
      </span></span><span>        <span>if ( have_posts() ) :
      </span></span><span>            <span>/* Start the Loop */
      </span></span><span>            <span>while ( have_posts() ) :
      </span></span><span>                <span>the_post();
      </span></span><span>            <span>endwhile;
      </span></span><span>        <span>endif;
      </span></span><span>        <span>?></span>
      </span>
      <span><span><span></body</span>></span>
      </span>

      This will remove the div tag and the duplication of the ul tag for us, but we still need to apply nav-item and nav-link to our menu items (to li and a tags respectively). How will we go about this? wp_nav_menu does not provide arguments for this. We’ll use the nav_menu_link_attributes and nav_menu_css_class filter hooks. We put this into our functions.php file:

      <span><span><?php
      </span></span><span><span>if ( have_posts() ) {
      </span></span><span>    <span>while ( have_posts() ) {
      </span></span><span>        <span>the_post();
      </span></span><span>        <span>//
      </span></span><span>        <span>// Post Content here
      </span></span><span>        <span>//
      </span></span><span>    <span>} // end while
      </span></span><span><span>} // end if
      </span></span><span><span>?></span>
      </span>

      Now we can specify new attributes in our wp_nav_menu in our header.php:

          <span><span><?php
      </span></span><span>    <span>if ( have_posts() ) : while ( have_posts() ): the_post(); ?></span>
      </span>
          <span><span><span><div</span> id<span>="post-<span><?php the_ID(); ?></span>"</span>></span>
      </span>        <span><span><span><h2</span>></span><span><?php the_title(); ?></span><span><span></h2</span>></span>
      </span>        <span><span><span><div</span> class<span>="post-excerpt"</span>></span><span><?php the_excerpt(); ?></span><span><span></div</span>></span>
      </span>    <span><span><span></div</span>></span>
      </span>
          <span><span><?php endwhile;
      </span></span><span>    <span>endif;
      </span></span><span>    <span>?></span>
      </span>

      Now our top menu links can take advantage of styles already defined in our Bootstrap theme’s CSS.

      Dynamic Header

      To be able to use a dynamic header — that is, a different header for the front page, for other selected pages, or for archives — we’ll define a dynamic_header() function in our functions.php file, where we’ll output our header markup dependent on the page the visitor loads.

      So now our header.php file will end like this:

      /**
       *
       * @package Botega_Scratch_Theme
       */
      
      get_header(); ?>
      
          <span><span><?php
      </span></span><span>    <span>if ( have_posts() ) : while ( have_posts() ): the_post(); ?></span>
      </span>
          <span><span><span><div</span> id<span>="post-<span><?php the_ID(); ?></span>"</span>></span>
      </span>        <span><span><span><h2</span>></span><span><?php the_title(); ?></span><span><span></h2</span>></span>
      </span>        <span><span><span><div</span> class<span>="post-excerpt"</span>></span><span><?php the_excerpt(); ?></span><span><span></div</span>></span>
      </span>    <span><span><span></div</span>></span>
      </span>
          <span><span><?php endwhile;
      </span></span><span>    <span>endif;
      </span></span><span>    <span>?></span>
      </span>
      
      

      We’ll also define that function like this:

      <span><span><?php
      </span></span><span><span>/**
      </span></span><span><span> * The header for our theme.
      </span></span><span><span> *
      </span></span><span><span> * <span>@package Botega_Scratch_Theme
      </span></span></span><span><span> *
      </span></span><span><span> */
      </span></span><span><span>?></span>
      </span><span><span><!DOCTYPE html></span>
      </span><span><span><span><html</span>></span>
      </span><span><span><span><head</span>></span>
      </span><span><span><span><meta</span> charset<span>="<span><?php bloginfo( 'charset' ); ?></span>"</span>></span>
      </span><span><span><span><meta</span> name<span>="viewport"</span> content<span>="width=device-width, initial-scale=1"</span>></span>
      </span>
      <span><span><?php wp_head(); ?></span>
      </span><span><span><span></head</span>></span>
      </span>
      <span><span><span><body</span> <span><span><?php body_class(); ?></span></span>></span>
      </span>
        <span><span><span><nav</span> class<span>="navbar navbar-default navbar-custom navbar-fixed-top"</span>></span>
      </span>    <span><span><span><div</span> class<span>="container-fluid"</span>></span>
      </span>      <span><span><span><div</span> class<span>="navbar-header page-scroll"</span>></span>
      </span>              <span><span><span><a</span> href<span>="<span><?php echo esc_url( home_url( '/' ) ); ?></span>"</span> rel<span>="home"</span> class<span>="navbar-brand"</span>></span><span><?php bloginfo( 'name' ); ?></span><span><span></a</span>></span>
      </span>      <span><span><span></div</span>></span>
      </span>
            <span><span><span><div</span> class<span>="collapse navbar-collapse"</span> id<span>="bs-example-navbar-collapse-1"</span>></span>
      </span>          <span><span><span><ul</span> class<span>="nav navbar-nav navbar-right"</span>></span>
      </span>          <span><span><?php wp_nav_menu( array( 'theme_location' => 'primary', 'items_wrap' => '%3$s' ) ); ?></span>
      </span>          <span><span><span></ul</span>></span>
      </span>      <span><span><span></div</span>></span>
      </span>    <span><span><span></div</span>></span>
      </span>  <span><span><span></nav</span>></span>
      </span>
        <span><span><span><div</span> class<span>="container"</span>></span>
      </span>      <span><span><span><div</span> class<span>="row"</span>></span>
      </span>

      To be able to use all the current URL or post data — like in the loop — we declare a $post variable global. Then we just fill different page or request cases with filler header HTML, which we’ll finish later. This sets the foundation for a truly dynamic header.

      We need to make sure that our front page — with dynamic top menu — will look good even when the user is logged in. WordPress shows an admin bar when visitors are logged in, even when they visit the front page. Because it has position: fixed, it overlays the top zone on our website, covering whatever is there, so we need to specify an offset for our top menu.

      We’ll add this to our style.css:

      <span><span><?php
      </span></span><span><span>/**
      </span></span><span><span> * Footer template partial
      </span></span><span><span> *
      </span></span><span><span> * <span>@package Botega_Scratch_Theme
      </span></span></span><span><span> *
      </span></span><span><span> */
      </span></span><span><span>?></span>
      </span>     <span><span><span></div</span>></span>
      </span>     <span><!-- /.row -->
      </span>  <span><span><span></div</span>></span>
      </span>  <span><!-- /.container -->
      </span>
        <span><!-- Footer -->
      </span>  <span><span><span><footer</span>></span>
      </span>    <span><span><span><div</span> class<span>="container"</span>></span>
      </span>      <span><span><span><div</span> class<span>="row"</span>></span>
      </span>          <span><span><span><div</span> class<span>="col-lg-8 col-md-10 mx-auto"</span>></span>
      </span>          <span><span><span></div</span>></span>
      </span>      <span><span><span></div</span>></span><!-- /.row -->
      </span>    <span><span><span></div</span>></span><!-- /.container -->
      </span>  <span><span><span></footer</span>></span><!-- /footer -->
      </span>
      <span><span><?php wp_footer(); ?></span>
      </span>
      <span><span><span></body</span>></span>
      </span><span><span><span></html</span>></span>
      </span>

      This makes sure the #mainNav — our menu container — has enough offset from the top, so it isn’t covered when user is logged in. WordPress adds logged-in and admin-bar classes to body in these cases, so we can easily target it.

      We can see that we address two cases in our CSS — one default, and another one for smaller screens. This is because WordPress outputs a wider admin bar on mobile devices, so we need to provide a 46px offset.

      On mobile, we should now have a responsive, JavaScript-powered dropdown menu:

      How to Build a WordPress Theme from Scratch: the Basics

      Conclusion

      In this second part on creating a WordPress theme from scratch, we created a very basic WordPress theme, and we included Bootstrap styles and scripts into it. We adjusted the menu output to fit our predefined styles. We also separated header and footer output into their respective partials.

      The functions.php file — a crucial file in theme development — is another topic we introduced and leveraged. Header output has been separated into its own function, which will use particulars of page visit, and site-owner–defined variables to determine the final output.

      In the third part of the guide, we’ll finish building particular templates, give better structure to our theme functions and partials, and finish up the styling of our website.


      There are three articles in this series on building a WordPress theme from scratch:

      • understanging a theme’s structure
      • theme basics
      • refining the theme

      Frequently Asked Questions (FAQs) about Building a WordPress Theme from Scratch

      What are the prerequisites for building a WordPress theme from scratch?

      Before you start building a WordPress theme from scratch, you need to have a basic understanding of HTML, CSS, PHP, and JavaScript. These are the core technologies used in WordPress theme development. Additionally, you should be familiar with the WordPress platform itself, including its file structure and template hierarchy. It’s also helpful to have a local development environment set up on your computer, such as MAMP or XAMPP, where you can test your theme as you build it.

      How do I start building a WordPress theme from scratch?

      The first step in building a WordPress theme from scratch is to create a new directory in your WordPress themes folder. This will be the home for all your theme files. Next, you’ll need to create a style.css file and an index.php file. The style.css file is where you’ll write all your CSS code, and it’s also where you’ll define your theme details. The index.php file is the main template file for your theme. It’s where you’ll write the PHP and HTML code that generates your website’s layout.

      How can I add custom functionality to my WordPress theme?

      You can add custom functionality to your WordPress theme by creating a functions.php file in your theme directory. This file acts like a plugin, allowing you to add custom features and functionality to your theme. You can use it to register navigation menus, add sidebars, enqueue styles and scripts, and much more.

      How do I create a responsive WordPress theme?

      To create a responsive WordPress theme, you’ll need to use media queries in your CSS code. Media queries allow you to apply different styles depending on the size of the user’s screen. This means you can create a different layout for desktop, tablet, and mobile devices. You’ll also need to make sure your images are responsive, which you can do by setting their width to 100%.

      You can customize the header and footer of your WordPress theme by creating a header.php file and a footer.php file in your theme directory. The header.php file is where you’ll write the HTML and PHP code for your header, and the footer.php file is where you’ll write the code for your footer. You can then include these files in your other template files using the get_header() and get_footer() functions.

      How do I add a custom post type to my WordPress theme?

      You can add a custom post type to your WordPress theme by using the register_post_type() function in your functions.php file. This function allows you to define a new post type with its own labels, capabilities, and features. You can then create a single-{posttype}.php file and an archive-{posttype}.php file to control the display of your custom post type.

      How do I add a sidebar to my WordPress theme?

      You can add a sidebar to your WordPress theme by creating a sidebar.php file in your theme directory and using the register_sidebar() function in your functions.php file. You can then display your sidebar in your other template files using the get_sidebar() function.

      How do I add a navigation menu to my WordPress theme?

      You can add a navigation menu to your WordPress theme by using the register_nav_menus() function in your functions.php file. This function allows you to register one or more navigation menus in your theme. You can then display your menu in your other template files using the wp_nav_menu() function.

      How do I customize the loop in my WordPress theme?

      You can customize the loop in your WordPress theme by modifying the loop code in your index.php file or other template files. The loop is the PHP code that WordPress uses to display posts. You can customize it to change the way posts are displayed, the number of posts displayed, and more.

      How do I update my WordPress theme?

      You can update your WordPress theme by making changes to your theme files and then uploading them to your WordPress site. If you’re using a child theme, you can update the parent theme without losing your changes. If you’re not using a child theme, you should make a backup of your theme before updating it, as updates will overwrite your changes.

      The above is the detailed content of How to Build a WordPress Theme from Scratch: the Basics. For more information, please follow other related articles on the PHP Chinese website!

      Statement of this Website
      The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

      Hot AI Tools

      Undress AI Tool

      Undress AI Tool

      Undress images for free

      Undresser.AI Undress

      Undresser.AI Undress

      AI-powered app for creating realistic nude photos

      AI Clothes Remover

      AI Clothes Remover

      Online AI tool for removing clothes from photos.

      Clothoff.io

      Clothoff.io

      AI clothes remover

      Video Face Swap

      Video Face Swap

      Swap faces in any video effortlessly with our completely free AI face swap tool!

      Hot Tools

      Notepad++7.3.1

      Notepad++7.3.1

      Easy-to-use and free code editor

      SublimeText3 Chinese version

      SublimeText3 Chinese version

      Chinese version, very easy to use

      Zend Studio 13.0.1

      Zend Studio 13.0.1

      Powerful PHP integrated development environment

      Dreamweaver CS6

      Dreamweaver CS6

      Visual web development tools

      SublimeText3 Mac version

      SublimeText3 Mac version

      God-level code editing software (SublimeText3)

      Hot Topics

      PHP Tutorial
      1488
      72
      How to diagnose high CPU usage caused by WordPress How to diagnose high CPU usage caused by WordPress Jul 06, 2025 am 12:08 AM

      The main reasons why WordPress causes the surge in server CPU usage include plug-in problems, inefficient database query, poor quality of theme code, or surge in traffic. 1. First, confirm whether it is a high load caused by WordPress through top, htop or control panel tools; 2. Enter troubleshooting mode to gradually enable plug-ins to troubleshoot performance bottlenecks, use QueryMonitor to analyze the plug-in execution and delete or replace inefficient plug-ins; 3. Install cache plug-ins, clean up redundant data, analyze slow query logs to optimize the database; 4. Check whether the topic has problems such as overloading content, complex queries, or lack of caching mechanisms. It is recommended to use standard topic tests to compare and optimize the code logic. Follow the above steps to check and solve the location and solve the problem one by one.

      How to minify JavaScript files in WordPress How to minify JavaScript files in WordPress Jul 07, 2025 am 01:11 AM

      Miniving JavaScript files can improve WordPress website loading speed by removing blanks, comments, and useless code. 1. Use cache plug-ins that support merge compression, such as W3TotalCache, enable and select compression mode in the "Minify" option; 2. Use a dedicated compression plug-in such as FastVelocityMinify to provide more granular control; 3. Manually compress JS files and upload them through FTP, suitable for users familiar with development tools. Note that some themes or plug-in scripts may conflict with the compression function, and you need to thoroughly test the website functions after activation.

      How to optimize WordPress without plugins How to optimize WordPress without plugins Jul 05, 2025 am 12:01 AM

      Methods to optimize WordPress sites that do not rely on plug-ins include: 1. Use lightweight themes, such as Astra or GeneratePress, to avoid pile-up themes; 2. Manually compress and merge CSS and JS files to reduce HTTP requests; 3. Optimize images before uploading, use WebP format and control file size; 4. Configure.htaccess to enable browser cache, and connect to CDN to improve static resource loading speed; 5. Limit article revisions and regularly clean database redundant data.

      How to use the Transients API for caching How to use the Transients API for caching Jul 05, 2025 am 12:05 AM

      TransientsAPI is a built-in tool in WordPress for temporarily storing automatic expiration data. Its core functions are set_transient, get_transient and delete_transient. Compared with OptionsAPI, transients supports setting time of survival (TTL), which is suitable for scenarios such as cache API request results and complex computing data. When using it, you need to pay attention to the uniqueness of key naming and namespace, cache "lazy deletion" mechanism, and the issue that may not last in the object cache environment. Typical application scenarios include reducing external request frequency, controlling code execution rhythm, and improving page loading performance.

      How to use the Plugin Check plugin How to use the Plugin Check plugin Jul 04, 2025 am 01:02 AM

      PluginCheck is a tool that helps WordPress users quickly check plug-in compatibility and performance. It is mainly used to identify whether the currently installed plug-in has problems such as incompatible with the latest version of WordPress, security vulnerabilities, etc. 1. How to start the check? After installation and activation, click the "RunaScan" button in the background to automatically scan all plug-ins; 2. The report contains the plug-in name, detection type, problem description and solution suggestions, which facilitates priority handling of serious problems; 3. It is recommended to run inspections before updating WordPress, when website abnormalities are abnormal, or regularly run to discover hidden dangers in advance and avoid major problems in the future.

      How to prevent comment spam programmatically How to prevent comment spam programmatically Jul 08, 2025 am 12:04 AM

      The most effective way to prevent comment spam is to automatically identify and intercept it through programmatic means. 1. Use verification code mechanisms (such as Googler CAPTCHA or hCaptcha) to effectively distinguish between humans and robots, especially suitable for public websites; 2. Set hidden fields (Honeypot technology), and use robots to automatically fill in features to identify spam comments without affecting user experience; 3. Check the blacklist of comment content keywords, filter spam information through sensitive word matching, and pay attention to avoid misjudgment; 4. Judge the frequency and source IP of comments, limit the number of submissions per unit time and establish a blacklist; 5. Use third-party anti-spam services (such as Akismet, Cloudflare) to improve identification accuracy. Can be based on the website

      How to enqueue assets for a Gutenberg block How to enqueue assets for a Gutenberg block Jul 09, 2025 am 12:14 AM

      When developing Gutenberg blocks, the correct method of enqueue assets includes: 1. Use register_block_type to specify the paths of editor_script, editor_style and style; 2. Register resources through wp_register_script and wp_register_style in functions.php or plug-in, and set the correct dependencies and versions; 3. Configure the build tool to output the appropriate module format and ensure that the path is consistent; 4. Control the loading logic of the front-end style through add_theme_support or enqueue_block_assets to ensure that the loading logic of the front-end style is ensured.

      How to add custom fields to users How to add custom fields to users Jul 06, 2025 am 12:18 AM

      To add custom user fields, you need to select the extension method according to the platform and pay attention to data verification and permission control. Common practices include: 1. Use additional tables or key-value pairs of the database to store information; 2. Add input boxes to the front end and integrate with the back end; 3. Constrain format checks and access permissions for sensitive data; 4. Update interfaces and templates to support new field display and editing, while taking into account mobile adaptation and user experience.

      See all articles