Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      CodeSOD: A Unique Way to Primary Key

      July 22, 2025

      BrowserStack launches Figma plugin for detecting accessibility issues in design phase

      July 22, 2025

      Parasoft brings agentic AI to service virtualization in latest release

      July 22, 2025

      Node.js vs. Python for Backend: 7 Reasons C-Level Leaders Choose Node.js Talent

      July 21, 2025

      The best CRM software with email marketing in 2025: Expert tested and reviewed

      July 22, 2025

      This multi-port car charger can power 4 gadgets at once – and it’s surprisingly cheap

      July 22, 2025

      I’m a wearables editor and here are the 7 Pixel Watch 4 rumors I’m most curious about

      July 22, 2025

      8 ways I quickly leveled up my Linux skills – and you can too

      July 22, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The Intersection of Agile and Accessibility – A Series on Designing for Everyone

      July 22, 2025
      Recent

      The Intersection of Agile and Accessibility – A Series on Designing for Everyone

      July 22, 2025

      Zero Trust & Cybersecurity Mesh: Your Org’s Survival Guide

      July 22, 2025

      Execute Ping Commands and Get Back Structured Data in PHP

      July 22, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

      July 22, 2025
      Recent

      A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

      July 22, 2025

      “I don’t think I changed his mind” — NVIDIA CEO comments on H20 AI GPU sales resuming in China following a meeting with President Trump

      July 22, 2025

      Galaxy Z Fold 7 review: Six years later — Samsung finally cracks the foldable code

      July 22, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Learning Resources»A Quick Guide to Escaping PHP Data in WordPress

    A Quick Guide to Escaping PHP Data in WordPress

    April 22, 2025

    Adding custom code to your WordPress website is powerful. You can add virtually any type of functionality. That’s great – but it also comes with responsibility.

    The output of your code must be secure. Otherwise, a malicious actor could take advantage. For example, they could execute a rogue JavaScript or PHP snippet to spread malware. It puts users at risk and is a mess to clean up.

    Thankfully, WordPress provides a way to prevent these sorts of attacks. Escaping data output strips unwanted and unsafe code. In other words, the feature ensures that only safe content will be output. It’s extra peace of mind when building custom themes and plugins.

    If you have coding knowledge, escaping PHP data output is relatively simple. However, it’s also an easy step to miss. That’s why improper code sanitization is one of the leading security issues in the WordPress ecosystem.

    Let’s change that narrative, one snippet at a time. Here’s a quick guide to escaping PHP data in WordPress. It could save you from a security nightmare or two.

    <!–


    Hosting DealsCheck out our collection of the best hosting for WordPress developers.

    –>


    WordPress.com vs WordPress.org
    WordPress.com vs. WordPress.org – What’s the difference?

    We get this question all the time, and we’re happy to help.

    • WordPress.org is the most powerful website building software on the web. You will need to find a hosting provider if you want that site online.
    • WordPress.com is our preferred hosting provider for medium-large traffic websites.

    If you want to know why WordPress.com is our preferred host for ambitious passion projects and large website projects, read our review:

    Our WordPress.com Review Migrate to WordPress.com

    A Way to Control and Safeguard Your Site’s Data

    What does data escaping do? Think of it as a filter in a coffeemaker. A filter’s job is to capture the ground-up beans and let the tasty liquid through. Likewise, escaping data filters out potentially dangerous bits of code before it’s rendered in a web browser.

    Let’s use a website contact form as an example. Our form might have fields for:

    • Name
    • Email Address
    • Postal Address
    • Message

    Each field has a specific purpose. We assume a user will add their first and last name to the “Name” field, and so on.

    But what if they don’t? We can’t guarantee that everyone will behave as expected. Assuming they will is a mistake.

    A malicious user or bot could try to add JavaScript to one or more of our fields. That code might be executed on our website. Any number of bad things could happen from there. The code could redirect you to a dangerous site or grab personal information such as passwords or payment details.

    Escaping the data collected by our form can prevent such shenanigans. The feature limits what input the fields will accept. It also prevents any disallowed input from being rendered.

    Like a coffee filter, data escaping cleans up our output while letting the good stuff flow through.

    Escaping data filters out potentially harmful input.

    WordPress Data Escaping Functions

    WordPress provides several functions for escaping data. Each is designed to handle different types of content.

    For instance, the esc_html() function will remove any HTML from the input. One example would be a user who includes HTML in a page title. This function ensures the code isn’t rendered in the output.

    Let’s say an adventurous user places the following in a page title field:

    <strong>About Us</strong>

    We haven’t escaped the PHP code in our theme:

    <h1><?php echo ( $title ); ?></h1>

    WordPress won’t remove the extra HTML in this scenario. That could be dangerous!

    <h1><strong>About Us</strong></h1>

    Now, let’s use esc_html() to strengthen our security:

    <h1><?php echo esc_html( $title ); ?></h1>

    The function will strip the HTML tags added by the user, leaving only the text:

    <h1>About Us</h1>

    Perhaps the code in our example is harmless. However, it opens the door to someone with more sinister intentions. Escaping data is a simple step that protects your website and everyone who visits it.

    Common Escaping Functions

    Let’s take a quick look at a few other escaping functions:

    esc_js() – This function is used to sanitize inline JavaScript code. It escapes special characters and prevents XSS attacks.

    <a target="_blank" href="#" onclick="alert('<?php echo esc_js( $message ); ?>')">Click me</a>

    esc_url() – Sanitizes URLs before output by escaping unsafe characters and validates HTTP/HTTPS protocols. You can also use esc_url_raw() to store URLs in the site’s database.

    <a target="_blank" href="<?php echo esc_url( $url ); ?>">Visit our site</a>

    wp_kses() – Escapes all non-trusted code while preserving HTML. Alternately, wp_kses_post() preserves code permitted in a post or page.

    <?php echo wp_kses($custom_field_content); ?>

    The above functions are among the most commonly used by developers. Check out the WordPress documentation for a complete list with code examples.

    Where Should You Use Escaping Functions?

    Escaping functions should be used anywhere you’re handling user input via code. That might be a theme template or a custom plugin. If you’re using custom fields, their output should also be escaped.

    Custom code is the primary focus here. If you’re unsure where to start, the Theme Check and Plugin Check plugins will help you find unescaped output in your code. From there, you can add an escaping function and test the changes.

    Note that most native WordPress functions like the_content() or the_title() are escaped by default – you don’t need to do anything further.

    What if you find some unescaped output in a third-party theme or plugin? Notify the developer! It could be an oversight on their part. Fixing it will help improve the product’s security.

    Escape to a Safer Place

    It’s easy to forget to escape output when writing code. Developers who aren’t well-versed in security may not even know about it. Code editor apps won’t alert you to the issue. And WordPress will execute your code regardless.

    So, keep this security feature in mind. Add it to a checklist or set a reminder in your project notes. It only takes a few extra seconds to implement. And it might be the difference in whether your site is as secure as possible.

    The post A Quick Guide to Escaping PHP Data in WordPress appeared first on Speckyboy Design Magazine.

    Source: Read More

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleSkywings Marketing: Best Digital Marketing Company in Ghaziabd
    Next Article 30+ Best Lightroom Presets for Stunning Portraits

    Related Posts

    Learning Resources

    I Made Kitty Terminal Even More Awesome by Using These 15 Customization Tips and Tweaks

    July 22, 2025
    Learning Resources

    What I learned from Inspired

    July 22, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    I took a 130-pound power station off-grid for a week – here’s my buying advice

    News & Updates

    FOSS Weekly #25.29: End of Ubuntu 24.10, AUR Issue, Terminal Tips, Screenshot Editing and More Linux Stuff

    Linux

    Splunk Enterprise XSS Vulnerability Let Attackers Execute Unauthorized JavaScript Code

    Security

    China-linked Salt Typhoon Exploits Critical Cisco Vulnerability to Target Canadian Telecom

    Development

    Highlights

    CVE-2025-37779 – “ERofs Linux Kernel Folio UAF Vulnerability”

    May 1, 2025

    CVE ID : CVE-2025-37779

    Published : May 1, 2025, 2:15 p.m. | 1 hour, 10 minutes ago

    Description : In the Linux kernel, the following vulnerability has been resolved:

    lib/iov_iter: fix to increase non slab folio refcount

    When testing EROFS file-backed mount over v9fs on qemu, I encountered a
    folio UAF issue. The page sanity check reports the following call trace.
    The root cause is that pages in bvec are coalesced across a folio bounary.
    The refcount of all non-slab folios should be increased to ensure
    p9_releas_pages can put them correctly.

    BUG: Bad page state in process md5sum pfn:18300
    page: refcount:0 mapcount:0 mapping:00000000d5ad8e4e index:0x60 pfn:0x18300
    head: order:0 mapcount:0 entire_mapcount:0 nr_pages_mapped:0 pincount:0
    aops:z_erofs_aops ino:30b0f dentry name(?):”GoogleExtServicesCn.apk”
    flags: 0x100000000000041(locked|head|node=0|zone=1)
    raw: 0100000000000041 dead000000000100 dead000000000122 ffff888014b13bd0
    raw: 0000000000000060 0000000000000020 00000000ffffffff 0000000000000000
    head: 0100000000000041 dead000000000100 dead000000000122 ffff888014b13bd0
    head: 0000000000000060 0000000000000020 00000000ffffffff 0000000000000000
    head: 0100000000000000 0000000000000000 ffffffffffffffff 0000000000000000
    head: 0000000000000010 0000000000000000 00000000ffffffff 0000000000000000
    page dumped because: PAGE_FLAGS_CHECK_AT_FREE flag(s) set
    Call Trace:
    dump_stack_lvl+0x53/0x70
    bad_page+0xd4/0x220
    __free_pages_ok+0x76d/0xf30
    __folio_put+0x230/0x320
    p9_release_pages+0x179/0x1f0
    p9_virtio_zc_request+0xa2a/0x1230
    p9_client_zc_rpc.constprop.0+0x247/0x700
    p9_client_read_once+0x34d/0x810
    p9_client_read+0xf3/0x150
    v9fs_issue_read+0x111/0x360
    netfs_unbuffered_read_iter_locked+0x927/0x1390
    netfs_unbuffered_read_iter+0xa2/0xe0
    vfs_iocb_iter_read+0x2c7/0x460
    erofs_fileio_rq_submit+0x46b/0x5b0
    z_erofs_runqueue+0x1203/0x21e0
    z_erofs_readahead+0x579/0x8b0
    read_pages+0x19f/0xa70
    page_cache_ra_order+0x4ad/0xb80
    filemap_readahead.isra.0+0xe7/0x150
    filemap_get_pages+0x7aa/0x1890
    filemap_read+0x320/0xc80
    vfs_read+0x6c6/0xa30
    ksys_read+0xf9/0x1c0
    do_syscall_64+0x9e/0x1a0
    entry_SYSCALL_64_after_hwframe+0x71/0x79

    Severity: 0.0 | NA

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    roobuilder – Vala and JavaScript IDE

    June 24, 2025

    Overwatch 2 Stadium Mode — Best Moira Builds: Best items, powers, and gameplay tips

    April 28, 2025

    Elden Ring Nightreign Gaping Jaw boss: How to beat Adel, Baron of Night

    May 30, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.