Wordpress Developers

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:
  • Overrides database value: 
    When defined in wp-config.phpWP_SITEURL takes precedence over the siteurl value stored in the WordPress database's wp_options table.
  • Reduces database calls: 
    Setting this constant can potentially reduce the number of database queries required to retrieve the site URL during page loads.
  • Temporary URL changes: 
    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.
  • Previewing on temporary domains: 
    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.phpConnect to your server using FTP or your hosting provider's file manager and locate the wp-config.php file in your WordPress installation directory.
  • Edit the file: Open wp-config.php in a plain text editor.
  • Add the definition: Add the following line before the line that says /* That's all, stop editing! Happy publishing. */: 
Code
    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.
Code
    remove_action('wp_head', 'wp_generator');
    add_filter('the_generator', '__return_empty_string');
  • Remove from Scripts and Styles (CSS/JS files):
Code
    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.php file.
  • 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.
  • Security Plugins: 
    Many security plugins like Sucuri Security include an option to hide the WordPress version number as part of their hardening features.
  • Dedicated Plugins: 
    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
  1. Access your WordPress files via FTP or your hosting file manager.
  2. Locate and open the wp-config.php file in the root directory.
  3. Find the line that reads /* That's all, stop editing! Happy publishing. */.
  4. Right before this line, add the following code: define('WP_MEMORY_LIMIT', '256M');.
  5. Save the file. 

Using .htaccess
  1. Access your WordPress files and locate the .htaccess file in the root directory (you may need to enable "Show Hidden Files" in your file manager).
  2. Make a backup of the file before editing.
  3. Add the following line to the file: php_value memory_limit 256M.
  4. Save the file and upload it back to your server. 

Other methods

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\_optionsStores site settings, configurations, and plugin options.
  • wp\_usersContains information about registered users, including usernames, passwords (hashed), emails, and display names.
  • wp\_usermetaStores additional metadata for users, such as user-specific settings or profile information.
  • wp\_postsStores all content types, including posts, pages, revisions, custom post types, and navigation menu items.
  • wp\_postmetaStores metadata associated with posts, pages, and custom post types (e.g., featured images, custom fields).
  • wp\_commentsStores all comments submitted on your posts and pages, whether approved or unapproved.
  • wp\_commentmetaStores additional metadata for comments.
  • wp\_termsStores categories, tags, and custom taxonomies.
  • wp\_term\_taxonomyDefines the taxonomy for the terms (e.g., whether a term is a category or a tag).
  • wp\_term\_relationshipsEstablishes 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.
Code
    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.
Code
    $query = $wpdb->prepare(
        "SELECT * FROM {$wpdb->prefix}posts WHERE post_status = %s AND post_type = %s",
        'publish',
        'post'
    );
  • %s is a placeholder for strings.
  • %d is a placeholder for integers.
  • %f is 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.
Code
        $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.
Code
        $row = $wpdb->get_row( $query );
  • $wpdb->get_var()Retrieves a single variable (e.g., a count).
Code
        $user_count = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->users}" );
  • $wpdb->get_col()Retrieves a single column from multiple rows as an array.
Code
        $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 or true/false for other queries.
Code
        $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.
Code
        $wpdb->insert(
            "{$wpdb->prefix}mytable",
            array(
                'column1' => 'value1',
                'column2' => 'value2',
            ),
            array( '%s', '%d' ) // Format of values
        );
  • $wpdb->update()Updates data in a table.
Code
        $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.
Code
        $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.
Akismet is designed to protect your website from comment spam by checking incoming comments against its global spam database. It analyzes millions of comments daily to improve its detection accuracy and helps keep your comment section clean. While Akismet is pre-installed, it requires activation and a connection to an Akismet account (which can be a free personal/non-commercial account) to function fully.

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:
Code
<?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_urlThis 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 .php or .html if 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 returns null.
  • Conditional CheckThe code then checks if a page object was returned. If $page is not null, it means the page was found, and you can access its properties like post_titleIDpost_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:
    • 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:
    • 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.phpAdd the following line of code to your theme's functions.php file:
Code
    add_theme_support( 'post-thumbnails' );
This line should typically be placed within a function hooked to after_setup_themeFor example:
Code
    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:
Code
    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.phparchive.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():
  • When you know the database has been modified by external means or another part of your code that doesn't use wpdb's caching mechanisms. 
    If you're directly interacting with the database outside of the standard wpdb methods, or if another system is modifying the data, flush() can ensure your wpdb queries reflect the latest state.
  • During development or debugging: 
    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:
Code
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'" );