Websites & Apps

{"type":"IMAGE"

DROPIDEA By Admin
June 1, 2025 7 views
DROPIDEA | دروب ايديا - {"type":"IMAGE"

WordPress uses a group of different languages ​​and basic programming technologies, learning which will help you take a step forward in dealing with WordPress, from simply creating and customizing WordPress sites to becoming a WordPress developer. The most important of these languages ​​are (HTML, CSS, and JavaScript), which operate on the client side, and PHP, which operates on the server side. The HTML language is responsible for determining the structure of the site, the CSS language is responsible for coordinating and designing its elements, and the JavaScript language is responsible for adding advanced interactive features such as scroll bars and other interactive features.

As for the PHP language, it is what enables the WordPress site to interact with the database and retrieve data from it in conjunction with a language responsible for querying the site’s database, such as MySQL or MariaDB. We have explained the basics of using HTML and CSS, just as we explained the JavaScript language and how to use it in the structure of the WordPress system, in previous articles. In today’s article, we will explain the basics of the PHP language, which is considered the basic language for developing WordPress. Contents of the article: What is the PHP language? What is the meaning of a server-side language or a client-side language?

How to write and implement PHP code?

What are the basic things a WordPress developer should learn in PHP? 1. Comments 2. Variables (Global and Superglobal Variables) 3. Constants (VARIABLES) 4. Conditional Statements 5. Include instruction 6. Loops 7. Functions User-defined functions Ready-made functions (built-in) 8. The concept of object-oriented programming (OOP) 9. Using namespaces Resources to learn more about the PHP language Using PHP within WordPress Conclusion A note before starting: It should be noted that this article does not aim to teach you PHP from A to Z, but it aims primarily to help you as a WordPress developer in obtaining basic knowledge that will help you recognize the basic rules and understand the programming code used by the WordPress system according to the latest available version of the language (PHP 8). What is the PHP language? PHP is an abbreviation for the term Hypertext P re_ P rocessor, meaning the hypertext preprocessor language. It is an open source programming language used for programming purposes in general, and for the purpose of developing websites in particular. PHP is a simple, lightweight server-side language that can be embedded in HTML code and other client-side scripting languages.

The PHP language also supports the concepts of object-oriented programming or OOP, which includes the concepts of classes, objects, inheritance, etc. Read more: Object Oriented Programming in PHP What is the meaning of a server-side language or a client-side language? As we mentioned previously, PHP is a server-side scripting language, unlike some other web development languages ​​that are client-side. So what does this mean?

Actually, web programming languages ​​are classified into two types: Client-side Programming Language Server-side Programming Language When you visit a web page written in a client-side language such as (HTML, CSS, or JavaScript), the code is sent directly to your browser so that the browser processes the information and outputs it in the form of a web page. Therefore, these languages ​​are considered client-side languages ​​because their processing occurs on the end user’s device. When you visit a web page written in PHP - or any other language on the client side - in this case, the PHP code is first processed by the server. That is, the entire PHP code is executed on the remote web server, then the result of this processing is obtained in the form of HTML code, after which the remote server sends this resulting page to the user's browser.

The user will receive HTML code representing dynamic pages containing the contents of the site, and these pages will be displayed on the Internet browser of his local device. How to write and implement PHP code? In order to run code server-side, you need a development environment that includes a server that can execute the code.

This development environment can be either local or remote. If you already have a web hosting account on a remote server you can run your code on it, or alternatively you can turn your computer into a local server by installing an emulator such as XAMPP. In this article, we will rely on running the codes locally without relying on a remote server because we will not need to be connected to the Internet to execute the codes. This speeds up the development process and allows us to see the changes immediately in the browser. It also enables us to test the code in more than one browser.

Read more: What is the XAMPP program and how to install and use it Creating a WordPress website on the local server (LocalHost) using the XAMPP program If you have installed WordPress on a local server, you are ready to implement PHP codes on your computer, and all you will need to write the codes is a text editor to write your code. You can use any simple text editor, such as Notepad, but a good text editor can speed up the development process, help you write PHP code and debug, and some code editors include support for WordPress. There are many suitable text editors that you can rely on such as Atom, Sublime Text, VSCode, PhpStorm, and NotePad++ and you can choose the one that suits you best.

In this article, we will use the VSCode editor because it provides several add-ons that facilitate writing code and support auto-completion of basic PHP functions as well as basic WordPress functions, and add-ons for fixing errors such as the PHP Intelephense WordPress Snippet add-ons, WordPress Toolbox, WordPress Development, and many others, which make it easier for you to write code for developing the WordPress system. The way PHP code is written is very similar to how HTML code is written.

She starts writing the hashtag

Between them your code instructions are written. Each PHP statement ends with a semicolon; But there is no need to put this semicolon in the last line of the code, that is, immediately after the line following the closing tag ?> The file must be saved with the extension .php, and the PHP file, as we mentioned, can contain HTML codes with embedded PHP codes. To write all the PHP codes, I will create a folder called ola inside the htdocs folder located inside the xampp folder, meaning the path will be as follows: C:xampphtdocsola in the following form and create a file inside it called demo.php. The htdocs folder is the root folder and is the location where Xampp will search for php codes, and the ola folder will be dedicated to collecting all the codes that I will implement in the explanations of this article.

Now I will open this entire folder that I created with the VS Code code editor, then I will edit the demo.php file and add the following code to it: Hello World PHP Page This code uses the echo print instruction, which prints the text string passed after it, and here it prints the phrase Hello World!

On the page. In order to see the result of executing the previous code, open the file through the browser (after making sure that the local server is running

Note 1: If the file contains code

PHP only (not embedded in HTML code) It is best to omit the PHP closing tag at the end of the file, as this prevents accidental whitespace or newlines from being added after the PHP closing tag which could cause unwanted effects. Note 2: If HTML code is included with PHP code nested in one file, any HTML code must be outside the php open and close tags, and any PHP code must be inside these two tags.

What are the basic things that a WordPress developer should do?

Learn it in PHP language? 1. Comments: Comments in any programming language are lines that are not implemented in the code, and their primary purpose is to explain how the code works to any other users who will use this code. Sometimes comments are used to disable part of the code so that it does not execute.

You can write comments in PHP in several ways, as follows: PHP has a set of high-level public variables. These variables are predefined and you can use them directly anywhere in the code without any prior declaration. For example, session data and cookies are stored in high-level global variables $_COOKIE and $_SESSION to be continuously available throughout the site. Note that the values ​​of the session variable $_SESSION are saved on the server, while the values ​​of the $_COOKIE variable are saved in the browser on the client side.

The WordPress system also provides a set of global variables to store various information. For example, information about the user, such as whether the user has logged in or not, and the type of browser through which the user visits the website, is stored in global variables. Information about the site's pages is also stored in global variables as well. For example, when browsing a specific publication, all the details of the current publication are stored in a global variable $post, and when browsing publications under a specific category, all these publications belonging to this category are stored in a global variable $wp_query, and so on, so that you can use them in your code. alert!

Working with global variable values ​​is important for understanding how WordPress works and developing themes and plugins. Although defining variables to be public is an easy way to share information throughout the website, they must be used with caution because they can be a vulnerability to sabotage your site. Being public means that anyone who uses them directly can change their value. 3. Constants (VARIABLES) A constant in a programming language is an identifier used to store a fixed value that cannot be changed during the execution of the code. Constants in PHP are case sensitive as in the case of variables, and it is preferable for their names to begin with uppercase letters.

It is not possible to assign a value to a constant directly. Rather, the constant is defined and its value is determined using the define() function, where we pass to this function the name of the constant and its value. The value of the constant can be read directly through its name or through the constant() function. Most uses of constants are usually in the code in order to store site settings, for example: Good night

Good day

The previous code displays either a good night or a good day message depending on the time the code is executed. It depends on the date(‘G’) function, which returns a number between zero and 23, where zero represents midnight and 23 represents 11 pm. If the value returned by the function is more than 18, this means that it is after 6 pm, so we display the message saying “Good Night” on the page, otherwise we display the message “Good Day.” 5. Include instruction: This instruction copies the contents of one file to another file, where the path to the required file is placed after the include keyword as follows: include 'path_to_file'; It is used on websites in general and on WordPress sites in particular when you want to include the same code in several places in a WordPress theme.

Through it, you can enter PHP statements into another file - before the server processes them. This instruction can greatly benefit you in avoiding duplication of the site's header, footer, and menu codes instead of writing their codes in multiple files or pages. You can place the header, footer, and menu code in separate files such as header.php, footer.php, and menu.php, and include them on the rest of the pages (other template template files). You can also upload the code from the functions.php file into the index.php file. You can use the include statement to do this.

If the file functions.php includes the following code, function names follow the same rules as other naming in PHP and are case insensitive. Since the function name can begin with a letter or an underscore, after which any number of letters, numbers, or underscores can be placed.

You can pass information to functions through a list of parameters that are passed in parentheses () after the function name and separated by commas. As of PHP 8, the list of function parameters can contain a comma at the end after the name of the last parameter. This comma is ignored, but it is useful if passing a large number of arguments, or if the parameter names are long, so in this case it is preferable to write the parameters vertically to make the code more readable. Built-in functions are functions built into the PHP language. PHP contains more than 1,000 built-in functions that can be called directly within the code to perform a specific task. (The WordPress core also adds its own ready-made built-in PHP functions) You can use them to expand and customize your code, and all you have to do is call them and pass the appropriate parameters to them.

But before calling any ready-made function, it is necessary to know how the function works, what it returns, and what values ​​you must pass to it to write correct PHP code. A list of all ready-made functions in PHP A list of all ready-made functions in the WordPress core For example, in the previous paragraphs we used several ready-made functions such as the date() function that returns the date and to which we can pass an argument or parameter representing the required date format. The have_posts() function returns a logical value of True if there are articles and False if there are none.

Other examples of ready-made functions in the WordPress core that are widely used are: get_post_type(): returns the post type, whether it is a default post in WordPress or a custom type post get_the_title(): returns the title of the post the_excerpt() returns the post snippet the_content() returns the post content the_tags() returns the post tags. There are many other ready-made functions that you can benefit from in WordPress development. At the following link is a list of the 100 most famous ready-made functions in WordPress 8. The concept of object-oriented programming (OOP) The PHP language supports an object-oriented programming language (OOP) for short, which is one of the relatively modern programming concepts that relies on the concept of classes or objects (Objects), which are sometimes called instances (Instances), where in object-oriented programming rows are created containing data and programming functions. Together, the class includes a set of properties that are usually private to the class so that they cannot be modified outside the class code, and a set of procedures or programming functions that are usually public so that they are called outside the class code, and the class acts as a template or model from which a group of different objects or instances can be created. Let's take a real-life example to understand the matter. If we manage to build a house, we first create a plan for the house.

The blueprint itself is not a house but an outline of a house. The blueprint can be thought of as a class. When an actual house is built based on this blueprint, the actual house is the object. Since we can build several identical houses from the same plan, in the same way we can create multiple objects from the same row, but each house can be customized according to our needs. For example, each house may have different paint colors and different interior decorations.

In short, a class is a blueprint for creating an object. Read more: Object Oriented Programming in PHP Now let's take a programming example that explains the matter. Suppose we want to create a row that represents an article or a blog post. The blog post will have properties such as the article ID, the title, the content itself, the name of the writer, whether it is published or not, the date of publication, etc. It will have its own actions or functions such as publishing, editing, and deleting the article.

We express this matter in PHP as follows: > We can conclude from reading the previous code that this code uses two ready-made functions in WordPress: the_ID() function, which brings us the ID of the current post, and the post_class() function, which returns the rows of a specific HTML element and will be useful to us in designing and formatting this article later. The following lines of the file include the following PHP code

DROPIDEA

We hope this article has added real value to you. At DROPIDEA, we always strive to deliver high-quality content that helps you grow and evolve in the digital space. Follow us for more useful articles and guides.

Share Article