Saturday, 27 Apr 2024
WORDPRESS

10 Useful WordPress Functions You Probably Not Know

Today in our article we are going to explain you some of the useful features of WordPress which can be useful to develop websites with this CMS. Many of these features will help us to use WordPress as more than just a CMS, with which we can develop web applications.

These functions go along with any plugin or theme that we develop. Surely you do not know most of the following functions and probably the next time you go to develop something you will save a lot of codes for using any of these functions. Let us start:

 

Get Unique File Name

If we want to save a file in a directory we must ensure that there is no longer the same name. WordPress has the function wp_unique_filename () to pass it a directory and filename. This function checks that the file exists and if there will add a number to the end ensuring that there is no such file.

$ Nombre_archio = wp_unique_filename ($ directory, $ filename);

Shows the Size of a File

This plugin is ideal for working with images or videos. These types of plugins are helpful to calculate the size of the files. WordPress function gives us size_format () to which we pass the size in bytes and it is about showing the size of the best. For example

size_format (1024) / / will return 1kb

size_format (1048576) / / will return 1 MB

Displays the Relative Time Format

This may be personal, but I do not like the type dates 13/05/2013 10:30 AM. Since I believe that the users don’t find anything through it. Prefer a relative time rate such as posted 3 days ago. WordPress has a function that is human_time_diff () that transforms us the date to this format automatically.

human_time_diff ($ from, $ to = NULL);

Check If User is Mobile

This is really useful. WordPress has a function in PHP that allows us to detect whether or not the user comes from mobile.

if (wp_is_mobile ()) {

echo “from the mobile user”;

Else {}

echo “User from desktop”;

}

Change the HTTP Status Code

For example, if we take the user to an error page, instead of using the default WordPress functions, we can use status_header () as follows:

status_header (‘404 ‘) / / 404 error page

Encodes to Avoid Spam Emails

There are many robots that are dedicated to surf the web pages looking for email addresses for later mind add to a database and send spam. With WordPress, using the function antispambot () can encrypt emails to print them on screen without robots that can scan them.

$ Email_codificado = antispambot ($ email);

Make clickable Links

WordPress comes with this feature that will turn our planes links clickable links below. Example:

$ String = “I’m reading http://guiabreve.com”;

miss make_clickable ($ string);

/ * Will print: I’m reading <a href=”http://guiabreve.com”> http://guiabreve.com </ a> * /

Make your own Slugs

WordPress uses a feature called `sanatize_title_with_dashes () ‘to generate all the slugs. We can use this function whenever we find it useful.

/ / Will print: this-is-a-title

Sanatize_title_with_dashes miss (“This is a title”);

Are we in SSL?

WordPress also gives us simple function to check if right now you’re using the SSL protocol.

if (is_ssl ()) {

echo “We are in SSL mode”;

}

Cut a String by Number of Words

We can use this function to generate our own excerpts or summaries of a string. A function wp_trim_excerpt () we pass the text to be cut and the number of words we want the resulting string.

$ Excerpt = wp_trim_words ($ text_largo, 100) / / 100 words