Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Prevent custom field from being updated after first publish

You are updating the audio_url regardless of one was found in the content or not.
Make it conditional:

if(preg_match_all ( "/(http|https):\/\/.*\/(.*)\.(mp3|m4a|ogg|wav|wma)/", $post->post_content, $matches )) {
    update_post_meta( $post_id, 'audio_url', $matches[0][0] ); 
}

In this case, you could also use preg_match, because you’re only looking for a single instance.

Also make sure that your regex does work correctly. Right now, it would get you wrong results if two URLs are in the document, since “.*” is greedy and will match as much as possible. Add a ? to change that behaviour to non-greedyness. While we’re at it, let’s remove the need to escape every Slash by using different delimiters:

"!(http|https)://.*?/(.*?)\.(mp3|m4a|ogg|wav|wma)!"

You don’t really need the parentheses around that second .*?, but maybe you have more code that needs it? Also, (http|https) could be written as https?, the question mark makes the previous character optional.

Related Posts:

  1. Add options to featured image
  2. Assign category using custom field?
  3. What WordPress function to use to get meta value by using meta keys?
  4. Default Custom Field Value Automatically Update
  5. How to change text color depending on the number value (Using javascript)
  6. Reference multiple style sheets, clearing styles for permalink page, custom fields for css
  7. Creating a “Related Meta” type field?
  8. update_post_meta not working in function
  9. Add custom fields after post/page title
  10. Adding and updating repeating custom field meta data
  11. How to display custom field in woocommerce orders in admin panel?
  12. Programatically add options to “add new” custom field dropdown
  13. Get a list of all available fields in the User Profile
  14. Enabling shortcodes for custom fields
  15. Detect meta value changes when post is updated (post_updated)
  16. Search everything (posts, pages, tags, cpt, meta)
  17. Change meta tags programatically
  18. Authors details such as social media links, emails etc → Is this Meta or something else?
  19. Use a function to update post meta based on other post meta
  20. post meta data clearing on autosave
  21. update_post_meta for custom field not working upon form submission
  22. How can update custom meta for all posts
  23. Function to Download External Images to My Site
  24. Checked() showing up twice in custom fields
  25. Convert User ID’s to User Names in a single.php file
  26. How to use max and min values of custom fields
  27. Woocommerce Storefront WordPress Ignore Media Gallery Images and use External 3rd Party Host
  28. Customizer field value into functions.php variable
  29. Can’t check if a post has thumbnail adding filter to get_post_metadata()
  30. Auto delete post if certain custom field data is empty
  31. Move Post to different category if post_meta field is 0 or is 2 days old?
  32. How to display custom option field in woocommerce orders in admin panel?
  33. How to add a slide toggle to the Woocommerce-Layout__Activity-Panel area in the orders admin page
  34. get_post_meta not working on publishing
  35. Removing custom meta data
  36. Change label for registration Fields
  37. Custom Attachment Caption Fields
  38. Post meta not updating
  39. Function to update post_meta based on existing post_meta
  40. WordPress function saves a post twice and updates all posts
  41. ACF not showing
  42. Exclude from the query posts with meta_key and meta_value
  43. How to retrieve the values of a sub-field in the first and last row of an (ACF) repeater inside function?
  44. How to get correct value from checked()?
  45. Get array of metakey in all posts
  46. Custom Behavior when Adding New Custom Post Type in Dashboard
  47. User function to return multiple get_post_meta()
  48. Hide custom fields when empty
  49. What are the meta fields for an attachment?
  50. Programmatically add Yoast meta description after post save
  51. Insert Address fields into function
  52. How to make custom comment fields required
  53. How add body_class from wp_termmeta
  54. How do I display a date correctly that is stored in the database as a backwards 8 digit number from Advanced Custom Fields? [closed]
  55. Sort and display pages with specific custom field (not tag)
  56. shorthand syntax for custom fields
  57. Fill empty custom field with other custom field
  58. Hide a div when a custom field is empty
  59. saving/reading custom field value does not work – no value gets POSTed
  60. Perform function on publish AND save (not just save)
  61. How to avoid saving empty data to sql while using add_meta_box
  62. How to Display WooCommerce Product Price in WooCommerce Archive
  63. How to add custom metakey to shop_order page’s searching function?
  64. wp_set_object_term via js btn frontend
  65. Populate Product Regular Price with a calculated ACF Field Value
  66. Update wp_postmeta table based on 2 keys
  67. blank page with wp_get_attachment
  68. How Can I Concatenate A String With One Of My Custom Field Value Before Saving The Post?
  69. Create New User Custom Field not Saving
  70. How To Display Author Popup on Entry Meta (Genesis Framework)?
  71. How would I correctly implement a new if statement in a child functions file?
  72. Hide empty custom field
  73. Remove post from latest posts after a month only with certain tag
  74. save_post doesn’t correctly process function call with php class
  75. How To Show Shortcodes In WordPress Custom Fields?
  76. Return ACF Field value function
  77. How to echo the translated custom field?
  78. How to add function to custom fields when page/post has a password?
  79. How to exclude posts with empty and non-existent relational custom field with meta_query?
  80. Post date on Custom field
  81. Dynamically generated Navigation Diagram using Custom Fields
  82. Define an extra field for all pages (with no plugin?)
  83. Adding custom field in menu options [duplicate]
  84. How To Get WordPress Categories Last Update Date?
  85. How to add a meta information to the URL?
  86. Automatically add custom fields (post_meta) to all published posts, hourly, via wp_cron?
  87. How to display data from custom fields in my custom shortcode?
  88. Syling Custom Fields echo’s from from functions.php
  89. update_post_meta after form is submited
  90. Display post_meta-by_key on product catalogue
  91. How to save custom made object in an array in a post meta field
  92. Change Post status based on custom field date +1 day
  93. A snippet after every image
  94. How can I add an image field to BuddyPress Extended Profile Fields? [closed]
  95. Override a theme function in a child theme?
  96. Include default functions and methods
  97. How to echo the value of an array element using a function via a shortcode
  98. Woocommerce custom calculation function
  99. button to toggle css styling / div visibility?
  100. Does WordPress function replace_hello() exists?
Categories functions Tags custom-field, functions, post-meta
How to hyperlink content from archive page with custom post-type
What is the difference between WordPress Business plan vs self hosted WordPress (WordPress developer perspective)? [closed]

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress