Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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).

0
votes
1answer
26 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
46 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
49 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
77 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
38 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
44 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
0answers
13 views

having trouble ignoring image tags in plugin

I have some code in a plugin I'm working on something like this: $string = preg_replace_callback("#([A-ZÄÖÜß?])#i", function ($matches) { $uppercount=null; $lowercount=null; // If the letter shall be ...
0
votes
1answer
45 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
56 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
160 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
28 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
0answers
86 views

Get Avatar Url Weird Result and Parse Avatar URL From get_avatar() Not Working

simple task here. I need to get the url of my user's avatar so I can use it as a background-image. I tried get_avatar_url() but it oddly always gave me the wrong url, going to a gravatar domain. I ...
0
votes
1answer
131 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
2k 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
96 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
43 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
0answers
43 views

SQL REGEX to match attachment IDs used in gallery shortcode

I need identify all posts that use a specific attachment in a gallery shortcode. To accomplish this I've written the following SQL query: // the attachment ID surrounded by word boundary chars // ...
0
votes
0answers
252 views

phone number validation regex

i wanted to change the regex format in my file from cpanel where i found a code such as: case 'number': $res .= "<input type='text' name='$key-$i' id='$key-$i' pattern='[0-9]{12}' maxlength='...
0
votes
1answer
185 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 ...
0
votes
0answers
139 views

Search and replace instagram link with shortcode having issues with wpautop

I am doing a search and replace on the_content of posts. This is because intagram embed has been failing randomly and I have decided to try to just search and replace the failed links with instagram ...
2
votes
0answers
2k 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
0answers
139 views

add_rewrite_rule filter combine 2 conditions/rules

I'm working on a project, where I have implemented custom rewrite rules: add_rewrite_rule('^store/([^/]*)/([^/]*)/([^/]*)/?','index.php?page_id=4&shop=$matches[1]&product_name=$matches[2]&...
0
votes
1answer
35 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
36 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
636 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
252 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
86 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
84 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
40 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
50 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
136 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
342 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
69 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
53 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
440 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
342 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
554 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#...
5
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
187 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
824 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
525 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
460 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
393 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
822 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
51 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 ...
0
votes
1answer
491 views

get_users meta_query: REGEXP not working for matching new lines

I have a meta field "my_meta_field" which may contain the following for these sample users: User A 99999 559225 12519 User B 5020492 99999 125191 User C 194295 1199999 Newlines are \r\n or \n. ...
6
votes
2answers
388 views

Insert Custom HTML After Shortcode

The scenario is that I need to add an HTML banner under a slideshow which is a shortcode in the homepage content. I personally hate adding a bunch of div tags to the TinyMCE editor so I'm trying to ...
1
vote
3answers
150 views

How do I remove all hyperlinks that begin with a #?

Facebook's Instant Articles rejects articles that contain hyperlinks to the current page (e.g. <a href="#_ftn1">[1]</a>). How can I filter the post content to remove these links before ...
0
votes
4answers
3k views

How to filter out shortcode when displaying the_excerpt() in the loop?

I'm using the_excerpt() in my template loop to display post excerpt on the front page. It's currently displaying unwanted shortcode directly on the front page eg. [box]post content[/box] [alert]...
2
votes
1answer
599 views

Getting results from wp_oembed_add_provider

I'm having trouble getting results from wp_oembed_add_provider(). I'm developing a site which needs to embed video content from Facebook, and I came across this handy list of Facebook URL structures. ...