The Stack Overflow podcast is back! Listen to an interview with our new CEO.

Questions tagged [regex]

Regular expressions (abbreviated regex) are terms that define a search pattern to focus on a specific part of a string for a specific purpose (e.g. to find/replace a word).

Filter by
Sorted by
Tagged with
1
vote
0answers
25 views

Preg Match All doesn't Allow echo first character for Wordpress Taxonomy Tags

I have been working on a code, that finds particular words from my content and converts them to wordpress Taxonomy Tags. I want to prefix them with a '#' symbol... But somehow it doesn't work This is ...
-1
votes
1answer
18 views

Apostrophes replaced by '

I use a plugin for automatic posting of articles on social networks, and when I send an article on Facebook or Twitter from my website, the apostrophes of my articles are replaced by this: ' ...
0
votes
0answers
14 views

Add a prefix to the Wordpress Home directory

I have a code like the following, <?php function link_fn( $url ){ $lang = get_query_var('lang', false); if( $lang !== false ) { $url = $...
0
votes
0answers
23 views

How to wrap pattern-matches from the_content in [wiki]-shortcode tags

I'm working on a function to wrap word-matches* in [wiki]-shortcode tags. I suspect the best method is to use add_filter() on the_content, and parse using RegEx in preg_replace_callback_array(), but ...
0
votes
1answer
71 views

rest api endpoint - accept diacritic characters

I am trying to register a custom endpoint, which will accept diacritic (accent) characters, so that I may search it in french. This is what I have tried: register_rest_route('events/v1', '...
0
votes
0answers
101 views

php preg_match with multiple patterns

I am trying to combine two preg_match patterns separate with ,|,. /blog\/page\/[0-9]+\/?$/ with /tag\/ skip in /blog/page/ and /tag/ /page\/[0-9]+\/?$/ with /[0-9]+\/?$/ ex. /page/2/ and only /2/ /(...
1
vote
1answer
21 views

What's the “?P<tag>” register rest api construct, what is for and where to find official docs?

I noticed that often in register_rest_routes, are used regexp like ?P<lang>, ?P<category_id>\d+ and so on, but I guess it's not standard regexp syntax. What exactly does it do?
0
votes
0answers
24 views

How to replace old YouTube embed code in Wordpress

I'm moving an old site from ExpressionEngine to Wordpress. The blog has over 3000 posts and some of them have YouTube videos embedded in them using code like this: <object width="425" height="344"&...
2
votes
1answer
22 views

Remove all table widths from editor content

When I paste a table into the WordPress editor, I always get widths like <td width="232">. I want to remove all widths when tables are added to the editor. If it was regex, I would write ...
3
votes
1answer
54 views

Custom post type permalink changing itself every other change

I have created a parent > child > grandchild structure. (Courses > Series > Episodes). I have created the below code which updates the urls for each of the custom post types so that I can have the ...
0
votes
0answers
32 views

Filter to APPEND nofollow to rel

I am trying append nofollow to add external links. If a link does not have a rel attribute, rel="nofollow" will be added. If a link already has rel="nofollow" nothing will happen, and if a link has ...
0
votes
1answer
41 views

Wordpress add_rewrite_rule with 2 variables

I need to create a rewrite rule that accepts 2 variables. I'm very new to this and am not sure If I'm even on the right track. If there is a better solution to what I'm trying to achieve, I'm open for ...
0
votes
3answers
184 views

Is there a way to get wp_editor (tinymce) content?

I want to get the wp_editor (tinymce) content then find a sentence by specific word with regex. However, have no idea how to get the content When a post is published or updated, the function will get ...
0
votes
1answer
236 views

Redirect 301 of old urls to wordpress urls

I am migrating a website from vivvo cms to wordpress. Vivvo is using category pages like index.numberofthepage.html I want to redirect urls like using regex: https://www.example.com/mycats/index.200....
0
votes
0answers
121 views

add_rewrite_rule not working. Returns 404

I'm adding custom URLs in Wordpress using add_rewrite_rule. Following is my code:- add_action( 'init', 'some_function' ); function some_function(){ global $wp_rewrite; add_rewrite_rule( // ...
0
votes
1answer
144 views

Removing WP custom post type slug causes 404 error with blog posts when blog does not have front slug

I've tried this wonderful solution (solution below, original post here) for removing the slug from a custom post type, but unfortunately it causes a 404 error for blog posts when the blog slug is ...
0
votes
1answer
78 views

Redirect regex misbehaving when placeholder empty

I have the following 2 redirects active: /code-examples/(.*) -> /tutorials/android/$1/ and /code-examples/android/(.*) -> /tutorials/android/$1/ The first one has a higher position, so the ...
0
votes
1answer
303 views

Extract links from content

I'm looking for a solution to only display the links inside a string (my wysiwyg content) on my single page. First of all extract all my links, and replace each links content by the links title ...
0
votes
1answer
148 views

Get just subdirectory name from a multisite URL

I want to get just the subdirectory name from the network site, and I've gone through the codex and cannot find a function that already does this. I have come up with a few options to get to the ...
2
votes
1answer
380 views

How to remove “YEAR/MONTH/” from uploads URL & redirect old URL to the new form?

I have disabled "Organize my uploads into month and year-based folders". The links to my PDF files were of the form: https://www.example.com/wp-content/uploads/2018/03/document.pdf And now they are:...
0
votes
1answer
44 views

How to add rewrite rule for product compare page?

I'm trying to create a product compare feature where we can compare three products, Two products must be added while the third is optional. Trying to use the add_rewrite_rule to create a friendly URL ...
0
votes
1answer
272 views

Ideas how to search & replace post_content when string contains a newline?

I've previously been able to search & replace using the very helpful "Search & Replace" plugin for wordpress but now i've came across a problem i cannot solve. In table wp_posts i want to ...
0
votes
1answer
5k views

Replace Dash (-) and Underscore ( _ ) with Space

I want to replace dashes & underscores with space in this image file name: text1_text2-10212-etc_125.jpg (result of a plugin) My code is: $string = preg_replace('/[\-_]/',' ', $file['name']); I'...
0
votes
2answers
149 views

Redirect after Permalink change - What regex do I use?

I'm completely out of my league with regex stuff. I've seen a lot of examples for putting redirect code into the .htaccess file, but the default examples are mostly for changing away from dates. ...
0
votes
1answer
96 views

HTML regex convert to titlecase

I have a plugin with an option to use regular expression and find and replace the title post. I need to convert lots of titles with uppercase and lowcase letters to title case, but I have no idea ...
0
votes
1answer
309 views

Redirect non www to www using redirection plugin regex

I have a scenario where I can't get access to the .htaccess file (no ftp/ssh ) so im relying on the Redirection plugin to redirect. I have an issue where i need to redirect non-www to www, while ...
2
votes
1answer
3k views

Adding Wordpress API Endpoint With Multiple Parameters

So I understand for the most part how the wp rest controller works, what it is doing and why it is the best way to do it. The trouble I am having is wrapping my head around the regular expressions in ...
0
votes
1answer
49 views

How to return tags containing one, two or three exact and specific words using get_tags

I'm using get_tags (outside the loop) with the argument name__like to return tags containing one, two or three exact and specific words. I already tried (not working): $args = get_tags( 'orderby' =&...
0
votes
0answers
37 views

Wordpress add_rewrite_rule

Struggling to get this working, i've never used regex before or played with .htaccess so i'm struggling to get this work. Eventually what i want to try and achieve is: mywebsite/vehicle-info/bmw/s6/...
0
votes
1answer
1k views

Remove External Links from WordPress posts Using add_filter() in Theme functions.php

We have been searching high an low for a plugin, code sample or example of how to remove all external links at the theme filter level but can't seem to find anything online. We want to keep the ...
1
vote
1answer
323 views

Conditional regex in add_rewrite_rule() for specific query filters & pagination

I'm working on a website with two post types sharing several taxonomies & custom fields. I've developed a filtering functionality with pretty urls, and urls for now works this way: 1) I've ...
1
vote
1answer
107 views

optimize wordpress rewrite rule regex

i have this codes in my wordpress plugin to add custom url rewrites: add_action('init', function() { add_rewrite_rule( '^cities/(.*)/browse/(.*)/(.*)/(.*)/(.*)', 'index.php?city_name=$...
0
votes
1answer
121 views

Call Shortcode Attribute Value within another function

Please anyone with an idea of how I can extract some values of this shortcode attributes and use it within another function. How the code looks like: function myVal($attr, $content = null) { ...
0
votes
1answer
42 views

Prevent WordPress from putting <p> around specific element

On my WordPress page I sometimes have the issues, that WordPress puts tags around elements, where I don't want them. For instance, I have this "invisible anchor" on many pages: <a class="anchor" ...
2
votes
1answer
64 views

Remove all categories from URL

This is the original I need to modify: https://example.com/actualites-cyclisme/criterium-du-dauphine/5115-dauphine-libere-le-parcours-devoile-lalpe-dhuez-fait-son-retour These are the things I have ...
0
votes
0answers
164 views

preg_replace style attr in $content and Editing post_content before saving

I`am trying to clean style attr from tags when saving posts and products but nothing happend. (I checked my regexp on https://regex101.com/) function filter_style_save($content) { $re = '/(<[^>]...
1
vote
0answers
428 views

Rewrite query string to path

I'm having a hard time trying to write regex for redirecting a query string to a path using rewrite rules like so: add_action('init', 'wp_j_url_rewrite'); function wp_j_url_rewrite() { ...
1
vote
1answer
79 views

Store regex expression in Wordpress DB using Options API

I have been searching all afternoon for a solution and couldn't find one. I am writing a plugin which needs to store regex expressions in the WP DB. An example expression is the following. /(http:\/...
0
votes
1answer
57 views

Stripping URLs & Email from post submissions

I want to prevent users posting in this instance of WordPress from submitting URLs and Emails in posts. I'm trying to do it via Regex but I'm not sure if I'm writing the Regex incorrectly because it ...
1
vote
1answer
635 views

Adding <div> Automatically To <table> In Wordpress Using Filter Referencing?

Does anyone know how I would automatically add <div class="table-responsive"> before every instance of a <table> on a WordPress site using filter referencing? I would also need to add a &...
1
vote
1answer
468 views

Extend PHP regex to cover “srcset” and “style” attributes

I've created a plugin that turn all links into protocol-relative URLs (removing http: and https:) based off the tags and attributes that I list in the $tag and $attribute variables. This is part of ...
4
votes
2answers
677 views

Wrap element with in any number of elements with PHP

I am working on a WordPress child theme where I do not have access to any markup other than the header.php. (I have access to child functions.php and the style.css) The site has a body and a div#...
4
votes
1answer
75 views

How to catch images with blank dimensions?

I've created a WordPress plugin that inserts width and height attributes to all images, for example: <img src="https://placehold.it/100x50"> - to - <img src="https://placehold.it/100x50" ...
0
votes
1answer
262 views

Get array of shortcodes within string

I do have an post/page where the shortcode "question" occurs many times. What is the best way to get an array of all the "question" shortcodes with their related parms? [question a=1 b=2 c=3 ] [...
1
vote
2answers
1k views

Wrap First Character after </h3><p> in a <span> tag

This is a continuation from a question I asked earlier about doing this with output from the_excerpt(). What I am trying to do is take the following output from the_content() to take the following ...
1
vote
1answer
618 views

How to set up redirects for category paginated pages after adding /category/ to permalink

We have a site using WPML and the category archives like /categoryname/page/2 weren't working in the secondary language. I added /category/ to the permalink and now the pagination works, but I need ...
1
vote
1answer
543 views

Gravity forms - get shortcode attribute from post content

I have some posts and some of them contain [gravityform] shortcode with different attribute values. Say, a post Hello World contains a shortcode [gravityform id="1" title="false" description="false"]...
0
votes
1answer
533 views

Rewrite permalink to include multiple taxonomies

I've seen similar questions posted before but they don't seem to quite fit my problem. I have a post type called product and I have 2 taxonomies, product_cat and location I want archive pages that ...
3
votes
1answer
1k views

Wordpress URL rewrite regex

I'm having trouble trying to learn how to write this url into a regex template to add in as a rewrite. I've tried various regex sandboxes to figure it out on my own but they won't allow a '/' for ...
0
votes
2answers
59 views

How to filter url on post submission?

I would like to swap out image URLs upon post submission on both content editor field and a custom field cover_image of a custom post type article. For example, the original content may contain image ...