Wordpress Developers
- wp_siteurl in config
- remove version number in WordPress
- to increase memory power in wordpress
- default table of WordPress
- to perform database query in WordPress
- wordpress default spam protection plugin default
- WordPress get page by url
- default taxonomies in wordpress
- support for post thumbnail
- Basics of Wordpress
wp_siteurl in config
In WordPress,
WP_SITEURL is a constant that can be defined in the wp-config.php file to specify the WordPress address (URL). This constant determines the location of your WordPress core files and is used by the site_url() API function.Purpose of
WP_SITEURL in wp-config.php:-
When defined in
wp-config.php,WP_SITEURLtakes precedence over thesiteurlvalue stored in the WordPress database'swp_optionstable. -
Setting this constant can potentially reduce the number of database queries required to retrieve the site URL during page loads.
-
It is particularly useful for temporarily changing the WordPress address, such as during site migration or troubleshooting when you cannot access the WordPress admin dashboard.
-
Allows previewing a site on a temporary domain without altering the database, preventing redirection to the old server.
How to define
WP_SITEURL:- Access
wp-config.php: Connect to your server using FTP or your hosting provider's file manager and locate thewp-config.phpfile in your WordPress installation directory. - Edit the file: Open
wp-config.phpin a plain text editor. - Add the definition: Add the following line before the line that says
/* That's all, stop editing!Happy publishing.*/:
define('WP_SITEURL', 'http://yourdomain.com');
remove version number in WordPress
To remove the WordPress version number, primarily for security and aesthetic reasons, you can implement one of the following methods:
1. Using Code in your Theme's
functions.php file:This method involves adding code snippets to your active theme's
functions.php file. Remove from Header and RSS Feeds. remove_action('wp_head', 'wp_generator');
add_filter('the_generator', '__return_empty_string');
- Remove from Scripts and Styles (CSS/JS files):
function remove_version_from_scripts_styles($src) {
if (strpos($src, 'ver=')) {
$src = remove_query_arg('ver', $src);
}
return $src;
}
add_filter('style_loader_src', 'remove_version_from_scripts_styles', 9999);
add_filter('script_loader_src', 'remove_version_from_scripts_styles', 9999);
Steps to implement:
- Access your WordPress Dashboard.
- Navigate to Appearance > Theme File Editor.
- Select your active theme and open the
functions.phpfile. - Add the desired code snippets to the end of the file.
- Click Update File to save your changes.
2. Using a Plugin:
Several plugins are designed to easily remove the WordPress version number without manual coding.
-
Many security plugins like Sucuri Security include an option to hide the WordPress version number as part of their hardening features.
-
Plugins like "Remove Version Info" or "Meta Generator and Version Info Remover" are specifically designed for this purpose.
Steps to implement (using a plugin):
- Access your WordPress Dashboard.
- Navigate to Plugins > Add New.
- Search for a suitable plugin (e.g., "Remove Version Info").
- Install and activate the chosen plugin.
- Follow the plugin's instructions to configure and hide the WordPress version number. This often involves a simple toggle or checkbox in the plugin's settings.
Verification:
After implementing either method, clear any caching and then view your website's source code (typically by right-clicking on the page and selecting "View Page Source" or using Ctrl+U/Cmd+U). Search for "WordPress" or "ver=" to confirm that the version number is no longer present in the meta generator tag or in the query strings of your CSS and JS files.
to increase memory power in wordpress
To increase the memory limit in WordPress, you can edit the wp-config.php file by adding
define('WP_MEMORY_LIMIT', '256M'); above the "That's all, stop editing!" line, or add php_value memory_limit 256M to your .htaccess file. You can also increase the limit through your hosting control panel or by contacting your hosting provider, who may have other methods available. Using
wp-config.php- Access your WordPress files via FTP or your hosting file manager.
- Locate and open the
wp-config.phpfile in the root directory. - Find the line that reads
/* That's all, stop editing! Happy publishing. */. - Right before this line, add the following code:
define('WP_MEMORY_LIMIT', '256M');. - Save the file.
Using
.htaccess- Access your WordPress files and locate the
.htaccessfile in the root directory (you may need to enable "Show Hidden Files" in your file manager). - Make a backup of the file before editing.
- Add the following line to the file:
php_value memory_limit 256M. - Save the file and upload it back to your server.
Other methods
- Hosting control panel: Log in to your hosting account and find the PHP settings (e.g., MultiPHP INI Editor in cPanel or PHP Configuration in hPanel). Change the
memory_limitthere and save your changes. - Contact your hosting provider: If the above methods don't work, your host may have specific instructions or can change the setting for you.
- Upgrade your hosting plan: In some cases, your current hosting plan may have a hard limit on memory, and an upgrade might be necessary to increase it further.
default table of WordPress
A default WordPress installation typically includes 12 core database tables, each serving a specific function to store and manage your website's data. These tables are usually prefixed with "wp_" by default, though this prefix can be customized for security reasons.
Here are the default WordPress tables:
- wp\_options: Stores site settings, configurations, and plugin options.
- wp\_users: Contains information about registered users, including usernames, passwords (hashed), emails, and display names.
- wp\_usermeta: Stores additional metadata for users, such as user-specific settings or profile information.
- wp\_posts: Stores all content types, including posts, pages, revisions, custom post types, and navigation menu items.
- wp\_postmeta: Stores metadata associated with posts, pages, and custom post types (e.g., featured images, custom fields).
- wp\_comments: Stores all comments submitted on your posts and pages, whether approved or unapproved.
- wp\_commentmeta: Stores additional metadata for comments.
- wp\_terms: Stores categories, tags, and custom taxonomies.
- wp\_term\_taxonomy: Defines the taxonomy for the terms (e.g., whether a term is a category or a tag).
- wp\_term\_relationships: Establishes the relationships between posts/pages and terms (e.g., which categories a post belongs to).
- wp\_links: (Less common in modern WordPress) Stores information about blogrolls or custom links.
- wp\_termmeta: (Introduced in WordPress 4.4) Stores metadata for terms.
to perform database query in WordPress
o perform database queries in WordPress, the primary method involves using the global
$wpdb object, which is an instance of the wpdb class. This class provides a set of methods for interacting with the WordPress database safely and efficiently.Here's a breakdown of common functions and their usage: Accessing the $wpdb object.
Before using any
$wpdb methods, you need to declare the global $wpdb object within your function or code block. global $wpdb;
Preparing Queries for Security.
It is crucial to use
$wpdb->prepare() to prevent SQL injection vulnerabilities when including dynamic values in your queries. $query = $wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}posts WHERE post_status = %s AND post_type = %s",
'publish',
'post'
);
%sis a placeholder for strings.%dis a placeholder for integers.%fis a placeholder for floats.{$wpdb->prefix}ensures you are using the correct table prefix for your WordPress installation.
- Executing Queries and Retrieving Data:
$wpdb->get_results(): Retrieves multiple rows as an array of objects or associative arrays.
$results = $wpdb->get_results( $query );
// or
$results = $wpdb->get_results( $query, ARRAY_A ); // for associative arrays
$wpdb->get_row(): Retrieves a single row as an object or associative array.
$row = $wpdb->get_row( $query );
$wpdb->get_var(): Retrieves a single variable (e.g., a count).
$user_count = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->users}" );
$wpdb->get_col(): Retrieves a single column from multiple rows as an array.
$post_titles = $wpdb->get_col( "SELECT post_title FROM {$wpdb->posts}" );
$wpdb->query(): Executes a generic SQL query (e.g., INSERT, UPDATE, DELETE). This method returns the number of affected rows for manipulation queries ortrue/falsefor other queries.
$rows_affected = $wpdb->query( $wpdb->prepare(
"DELETE FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = %s",
13,
'stars'
));
- Inserting, Updating, and Deleting Data (Convenience Methods):
The
$wpdb class also offers convenience methods for common data manipulation tasks, which internally use $wpdb->prepare() for security.$wpdb->insert(): Inserts data into a table.
$wpdb->insert(
"{$wpdb->prefix}mytable",
array(
'column1' => 'value1',
'column2' => 'value2',
),
array( '%s', '%d' ) // Format of values
);
$wpdb->update(): Updates data in a table.
$wpdb->update(
"{$wpdb->prefix}mytable",
array( 'column1' => 'new_value' ), // Data to update
array( 'id' => 1 ), // WHERE clause
array( '%s' ), // Format of updated values
array( '%d' ) // Format of WHERE clause values
);
$wpdb->delete(): Deletes data from a table.
$wpdb->delete(
"{$wpdb->prefix}mytable",
array( 'id' => 1 ), // WHERE clause
array( '%d' ) // Format of WHERE clause values
);
wordpress default spam protection plugin default
The default spam protection plugin that comes pre-installed with every WordPress installation is Akismet Anti-spam.
WordPress get page by url
To get a WordPress page by its URL, you can use the
get_page_by_path() function. This function retrieves a page object based on its slug or full path.Here's how to use it:
<?php
// Define the full URL of the page
$page_url = 'https://example.com/about-us/our-team/';
// Extract the path from the URL
$path = parse_url( $page_url, PHP_URL_PATH );
// Remove leading/trailing slashes and any file extensions if present
$path = trim( $path, '/' );
$path = preg_replace( '/\.(php|html)$/', '', $path ); // Remove .php or .html if present
// Get the page object by its path
$page = get_page_by_path( $path );
if ( $page ) {
// Page found, you can now access its properties
echo 'Page Title: ' . $page->post_title;
echo 'Page ID: ' . $page->ID;
// You can access other properties like $page->post_content, $page->post_status, etc.
} else {
echo 'Page not found for the given URL.';
}
?>
Explanation:
$page_url: This variable holds the full URL of the page you want to retrieve.parse_url( $page_url, PHP_URL_PATH ): This extracts only the path component from the full URL (e.g.,/about-us/our-team/).trim( $path, '/' ): This removes any leading or trailing slashes from the path.preg_replace( '/\.(php|html)$/', '', $path ): This removes any common file extensions like.phpor.htmlif they are present in the path. This ensures you're left with the clean slug or path that WordPress uses.get_page_by_path( $path ): This is the core WordPress function. It takes the cleaned path (or slug) as an argument and returns the corresponding page object if found. If no page matches the path, it returnsnull.- Conditional Check: The code then checks if a page object was returned. If
$pageis notnull, it means the page was found, and you can access its properties likepost_title,ID,post_content, etc.
This method allows you to reliably retrieve a WordPress page object using its permalink structure.
default taxonomies in wordpress
WordPress includes two primary default taxonomies for organizing content, specifically for the "post" post type:
-
- Categories are hierarchical, meaning they can have parent and child relationships, allowing for a structured organization of content (e.g., "News" as a parent category with "Local News" and "International News" as child categories).
- Every WordPress post must be assigned to at least one category. By default, new posts are assigned to the "Uncategorized" category if no other category is selected.
-
- Tags are non-hierarchical, providing a more flexible and granular way to describe specific keywords or topics within a post.
- Tags do not have parent-child relationships and can be applied freely to posts to indicate relevant subjects.
These default taxonomies are fundamental to content organization in WordPress, enabling users to group and classify posts for improved navigation and discoverability. While these are the built-in defaults, WordPress also allows for the creation of custom taxonomies to suit specific content organization needs beyond categories and tags.
support for post thumbnail
o enable support for post thumbnails (also known as featured images) in a WordPress theme, the theme must explicitly declare this support.
Steps to Enable Post Thumbnail Support:
- Modify
functions.php: Add the following line of code to your theme'sfunctions.phpfile:
add_theme_support( 'post-thumbnails' );
This line should typically be placed within a function hooked to
after_setup_theme. For example: function mytheme_setup() {
add_theme_support( 'post-thumbnails' );
}
add_action( 'after_setup_theme', 'mytheme_setup' );
- Enable for Specific Post Types (Optional): If you want to enable post thumbnails only for specific post types (e.g., 'post', 'page', or a custom post type), you can specify them in an array:
add_theme_support( 'post-thumbnails', array( 'post', 'page', 'my_custom_post_type' ) );
- Verify in WordPress Admin: After adding this code, navigate to the "Edit Post" or "Edit Page" screen in your WordPress admin. You should now see a "Featured Image" metabox in the right sidebar. If it's not visible, ensure that "Featured Image" is enabled in the "Screen Options" at the top right of the screen.
Displaying Post Thumbnails in Your Theme:
Once enabled, you can display the featured image in your theme's templates (e.g.,
single.php, archive.php) using functions like:the_post_thumbnail(): Displays the featured image.get_the_post_thumbnail(): Returns the HTML for the featured image, allowing for more customization.
Basics of Wordpress
Basics of Wordpress
wpdb::flush
The
wpdb::flush() method in WordPress is used to clear the internal cache of the wpdb object.When you perform database queries using the
wpdb class (e.g., $wpdb->get_results(), $wpdb->query()), WordPress often caches the results of these queries internally within the wpdb object to improve performance. This means that if the same query is executed multiple times within a single request, the cached result might be returned instead of re-querying the database.Calling
wpdb::flush() will clear this internal cache, forcing wpdb to re-query the database for subsequent operations, even if the same query was previously executed and cached.When to use
wpdb::flush():-
If you're directly interacting with the database outside of the standard
wpdbmethods, or if another system is modifying the data,flush()can ensure yourwpdbqueries reflect the latest state. -
If you're seeing unexpected cached results and need to ensure you're always getting fresh data from the database,
flush()can be helpful.
Example:
global $wpdb;
// Perform a query, results are cached
$results1 = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}options WHERE option_name = 'my_option'" );
// Clear the wpdb internal cache
$wpdb->flush();
// Perform the same query again, it will now re-query the database
$results2 = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}options WHERE option_name = 'my_option'" );