Get array of shortcodes within string

function wpse250308_get_questions() { global $post; if ( preg_match_all(‘/\[question(.*?)\]/’, $post->post_content, $questions ) ) { $questions = array_key_exists( 1 , $questions) ? $questions[1] : array(); // $questions will contain the array of the question shortcodes //Do your stuff } } $post isn’t available before wp. So, you have to hook on it or actions that fires later.

Stripping URLs & Email from post submissions

You can use the_content (Click here for more info) hook to change the content while rendering the post/page. This will be triggered when Post is being read from Database. So post content will not be changed in database but only filtered while rendering. add_filter( ‘the_content’, ‘remove_email_and_url_from_post’ ); function remove_email_and_url_from_post( $content ) { // Check if … Read more

Redirect non www to www using redirection plugin regex

You should try changing your WordPress Site URL from Non WWW to WWW (Eg. if your site url is http://example.com then you should edit it from Setting > General > WordPress URL to http://www.example.com I think this will solve your problem.

Extract links from content

I thought regex search may become critical so html dom parser may help. See the code below (if it helps): <?php $post_content = get_the_content(); $dom = new DomDocument(); $dom->loadHTML($post_content); foreach ($dom->getElementsByTagName(‘a’) as $item) { echo “<a href=”https://wordpress.stackexchange.com/questions/315648/.$item->getAttribute(“href’).” target=”_blank” rel=””. $item->getAttribute(“rel’) .”‘>”. $item->getAttribute(‘title’) .”</a><br />”; } ?>

Redirect regex misbehaving when placeholder empty

The problem with your RegEx patterns: /code-examples/(.*) /code-examples/android/(.*) is that they only match when the ending slash (/) is present in the URL; e.g.: # Example 1: $1 is ‘page1’ /code-examples/page1 /code-examples/android/page1 # Example 2: $1 is ” (empty) /code-examples/ /code-examples/android/ # Example 3 # No matches because the ending / is not present. /code-examples … Read more

Redirect 301 of old urls to wordpress urls

Since category and page number is dynamic, you can use htaccess and use this sample rewrite rule below: RewriteRule (.*?)/index.(.*?)\.html $1/page/$2/ You can test the regex here – https://htaccess.madewithlove.be/ and I can see that it is working as far as I have tested it. PS: URL Redirections are cached aggresively, you need to clear you … Read more

php preg_match with multiple patterns

I think this is what you are looking for if (preg_match(‘@^/((?!blog|tag)[^/]+)/(?:page/)?\d+@’, $_SERVER[‘REQUEST_URI’], $m)) { wp_redirect(“/$m[1]”, 301); } The regexp uses lookahead to make sure that the first match isn’t blog or tag. The ‘[^/]+’ part matches anything that isn’t a forward slash (/). (?:page/)? – Makes an optional match against ‘page/’ \d+ – Match against … Read more

enqueue script with get_shortcode_regex

There’s already a function for checking if a post has a shortcode: has_shortcode(): function enqueue_script_if_shortcode_is_detected() { global $post; wp_register_script( ‘leweb-widgets’, get_stylesheet_directory_uri() . ‘/includes/leweb-widgets.php’, ‘1.0’, true ); if ( has_shortcode( $post->post_content, ‘leweb_suggested_events’ ) ) { wp_enqueue_script(‘leweb-widgets’); } } add_action( ‘wp_enqueue_scripts’, ‘enqueue_script_if_shortcode_is_detected’ ); Just keep in mind that checking the post this way: Will not know if … Read more

How to add no follow to specific links?

The replace code is replacing the whole a element content with <a rel=”nofollow”>. Here’s the correction: I used (.+?) between <a and href” and before and after /go/pluto/ to the closing >. Then in the replace, we put $1 $2 $3 etc. to keep them and add rel=”nofollow”. You can check in this test, and … Read more