Hot answers tagged

11

You can add your own rewrite rule which will let you tweak the query via URL parameters: add_action( 'init', 'rewrite_photo_url' ); function rewrite_photo_url() { add_rewrite_rule( 'photos/([^/]+)/?$','index.php?page=photos&photo_id=$matches[1]', 'top' ); } If you need to use a custom variable, i.e. 'photo_id', you have to register the variable so ...


10

Add this to your plugin or functions.php file of your theme. The example below will include 'your_key' in search. You can include all your keys by repeating the array. function me_search_query( $query ) { if ( $query->is_search ) { $meta_query_args = array( array( 'key' => 'your_key', 'value' => $query->query_vars['s'] = '', ...


8

You need to do this in steps: Decide when you are going to parse the user's meta to change the value. Define a function to do that. Hook that function to the appropriate action. The original answer defined what you need to do for step #2, using preg_match() to parse the Twitter URL and extract the username. function update_the_user( $user ) { // ...


7

Method #1 If available, I would use the: shortcode_atts_{$shortcode} filter to collect the attributes of a given shortcode. Example: $text = ' [gallery] [gallery ids="1,2" link="file"] [gallery ids="3"] [caption id="attachment_6" align="alignright" width="300"] '; if( class_exists( 'WPSE_CollectShortcodeAttributes' ) ) { $o = new ...


6

The filters you are trying to use run on image insertion, so it is not possible to swap all the images already present in your posts using these filters. It should work, however, if you intend to change to img tags from now on. The filter the_content, however, is applied to the post after it is retrieved from the database and before displaying it to screen. ...


5

You say, I do not have access to anything other than the style-sheet and a header.php. ...but you do not state why this is. It seems to me that for a child theme to work well, you'd need at least a functions.php if not a template for content as well. If you can't access those or create those, I would say to your client, "Here's your shitty website. This ...


4

You can use add_rewrite_tag to register your custom query variable ('id' in question): add_rewrite_tag('%id%','([0-9]+)'); (The regex tells it only to accepts digits). Then to create your rewrite rule you can use add_rewrite_rule (both of these should be hooked onto init). add_rewrite_rule('^photos/([0-9]+)/?','index.php?p=1234&id=$matches[1]','top');...


4

I read your question differently than @eric-holmes. It sounds to me like your shortcode needs to function normally under most circumstances but that you are extracting information in special circumstances. Shortcode regex is tricky. Let WordPress do it for you. $pattern = get_shortcode_regex(); preg_match_all("/$pattern/",$wp_content,$matches); Your ...


4

I wonder if you could override the [rev_slider] with this kind of wrapper: add_shortcode( 'rev_slider', function( $atts = array(), $content = '' ) { $html = ''; // Your custom banner HTML $banner = '<div id="bannerHTML"><!-- banner HTML goes here --></div>'; // Append your banner HTML to the revslider's output if( ...


3

The task It's not as easy, as it looks at a first glance. The main problem is, that you can define your own [gallery] shortcode and simply override the default. Actually that's what some themes are doing (blame you ThemeForest authors!). In this case, simply hooking into post_gallery won't work in every case. Drawbacks for post_gallery The problem is that ...


3

You are simply going to love this one First of all, here is a sample table with data loaded: mysql> use junk Database changed mysql> drop table todd; Query OK, 0 rows affected (0.01 sec) mysql> create table todd (id int not null auto_increment,url VARCHAR(255), -> primary key (id)) ENGINE=MyISAM; Query OK, 0 rows affected (0.05 sec) mysql&...


3

Where is this string located ? I know you wrote "without wordpress system" , but WordPress has a function get shortcode regex If you really want to avoid it for some reason, the following example might help you : // check the post for a short code function check_shortcode( $shortcode = NULL ) { $post_to_check = get_post( get_the_ID() ); // ...


3

Sorry, brackets are still not allowed within shortcodes, as you can see in the Shortcode API. However it's possible to use enclosing shortcodes which will allow to use brackets. Let me demonstrate this: Shortcode: [foo bar="No brackets here!"]...use [brackets] here[/foo] You can add this kind of shortcode in your functions.php like this: add_shortcode( '...


3

You should probably not run preg_replace on the meta data for a _wp_attachment_metadata field as the data is serialized. I would recommend instead you write a small script to loop through the images, unserialize the data, update the value, serialize and store the modified array. Here's a sample function which does just that: function ...


3

The major issue is just that you have a positive lookbehind but no capture group so \1 or $1 isn't a usable variable. Fix your regex to provide the capture group: '/(?<=\<\/h3\>\n<p>)(.)/' then reference as $1. ob_start();?> <h3>Heading</h3> <p>Paragraph</p> <p>Paragraph2</p> <?php $...


3

Personally, the first thing I would do after matching the attributes is to create an associative array of: array( 'attribute_name' => 'attribute_value' ) It will make it easier to get the attribute directly than having to search the array and I think overall it will make the code more direct. Since we know that index 0 is our attribute names and index 1 ...


3

Can I use a similar technique to 'wrap' my everything in the body with another div? Absolutely. Almost everything is possible. But you're going to have to do something a little hackish. The the_content filter doesn't actually filter the content of the entire page. That particular filter is used throughout WordPress to filter a variety of different contents....


2

This is an improved Ahmed's answer: function me_search_query( $query ) { if ( $query->is_search ) { $meta_query_args = array( array( 'key' => 'your_key', 'value' => $query->query_vars['s'], 'compare' => 'LIKE', ), ); $query->set('meta_query', $meta_query_args); add_filter( 'get_meta_sql'...


2

If your up for using a plugin, Relevanssi - A Better Search might be worth trying out. The standard (free) version supports searching post meta.


2

Let's see, this should do the trick then: RewriteCond %{REQUEST_URI} \.html$ RewriteCond %{REQUEST_URI} !^/specialfolder/ RewriteRule (.*)\.html http://www.example.com$1 [R=301,L]


2

Depending on how the rules are being generated, you can use the get_query_var() function to get the value of the 'photo' query var. If it's done properly, 'photo' should be an available query var. You'll need to sanitize the value with intval() or similar of course.


2

Two points: Your rule isn't particularly specific. For numeric matches you should be specific about it and specify a) digits and b) how many digits. Year would be ([0-9]{4}), month/day would be ([0-9]{1,2}). You can't do it with one rule. Add three separate rules instead. add_rewrite_rule( 'whats-on/([0-9]{4})/?$', 'index.php?page_id=71&event_year=$...


2

Just use wp_localize_script() and is_user_logged_in(). wp_localize_script( 'my-ajax-request', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'logged_in' => is_user_logged_in() ) );


2

There's no need to use regex for this. WordPress has a Shortcode API to do this work for you. That codex page includes one of my favorite codex code snippets: function bartag_func( $atts ) { extract( shortcode_atts( array( 'foo' => 'something', 'bar' => 'something else', ), $atts ) ); ... Right at the start of the ...


2

You can use regex anywhere you have a string to manipulate. That is basic PHP. There is nothing special about WordPress that changes that. But why use regex when there are other options? As much fun as it is, regex is tricky and easy to get wrong, and there is significant overhead to using it. What you are doing generates an "array to string conversion" ...


2

I copied your code to a file in my home directory on my Ubuntu box, removed the weird character that @s_ha_dum noticed, and ran it. It worked as I'd expect. Code: <?php function get_the_post_imgs($oldPostContent) { $newPostContent = $oldPostContent; $newPostContent = preg_replace_callback( "/<img.*src=[\"']([^\"']*)[\"'].*>/i", //...


2

I think you are talking about a rare scenario. "font-awsome" is the expected name that all plugins and themes should be using as handle. If the developer has not changed the source code of font awsome CSS file, there is no reason to change the name of the handle. Anyway, you can check the global $wp_styles object and make a regex in the src value of each ...


2

Don't try to parse the shortcodes and then regex the HTML. Use get_shortcode_regex() to parse the raw post content: $content = $content_post->post_content; preg_match_all("/$pattern/",$content,$matches); Then crawl $matches to find your shortcode data. Use shortcode_parse_atts($matches[3][0]) (note $matches[3][0] to give the first element within the ...


2

Try this. You also don't want to include $wp_rewrite->flush_rules() inside your function as that would flush on every page load. add_action('rewrite_rules_array', 'new_rewrite_rules'); function new_rewrite_rules($rules){ $newrules = array(); $newrules[ '^lists/([^/]*)/([^/]*)/?' = 'index.php?lists=$matches[1]&post=$matches[2]'; ...


Only top voted, non community-wiki answers of a minimum length are eligible