Tag Info

New answers tagged

1

Ok, so the problem was that I was adding the variable outside the function. After some cleanup, code looks like this and it works! function roots_setup() { // Variables $header_type = get_field('header_type','options'); if (($header_type)=='brand-centered') { register_nav_menus(array( 'brand_centered_left' => __('Brand Centered Navigation (Left)', ...


2

Anyway, basically Hody_McGee gave the answer in his comment: You could use wp_localize_script(). As it states in the Codex: [wp_localize_script()] can be used to make any data available to your script that you can normally only get from the server side of WordPress. How do we do this? <?php add_action( 'wp_enqueue_scripts', 'register_scripts' ...


7

This is an alternative approach to @kaiser answer, that I found pretty fine (+1 from me) but requires additional work to be used with core WP functions and it's per-se low integrated with template hierarchy. The approach I want to share is based on a single class (it's a stripped-down version from something I'm working on) that takes care of render data for ...


1

An easy solution is to write a function to get the extra title. I use a static variable to keep the database calls to one only. Put this in your functions.php. function get_extra_title($post_id) { static $title = null; if ($title === null) { $title = get_post_meta($post_id, "_theme_extra_title", true) } return $title; } Outside ...


0

In case you use a "child theme", i would search also in the parent themes function.php @see https://codex.wordpress.org/Child_Themes


4

Simple answer, don't pass variables anywhere as it stinks of using global variables which is evil. From your example it seems like you are trying to do an early optimization, yet another evil ;) Use the wordpress API to get data which is stored in the DB and don't try to outsmart and optimize its usage as the API do more then just retrieving values and it ...


0

Although kaiser's answer is technically right, I doubt it's the best answer for you. If you're creating your own theme, then I think it is indeed the best way to set up some sort of framework using classes (and maybe namespaces and interfaces too, although that might be a little too much for a WP theme). On the other hand, if you are just extending / ...


6

Basic separated data structures To pass around data, you normally utilize a Model (that's the "M" in "MVC"). Let's look at a very simple interface for data. Interfaces are just used as "Recipes" for our building blocks: namespace WeCodeMore\Package\Models; interface ArgsInterface { public function getID(); public function getLabel(); } Above is ...


0

It was simpler than I thought. This has solved it. <?php add_filter('the_content_more_link', 'modify_read_more_link'); function modify_read_more_link() { return '<a class="more-link" href="' . get_permalink() . '">Your Read More Link Text</a>'; } the_content("Read More"); ?>


0

You can still do the AS with $wpdb, I find it still makes stuff look cleaner. You can use the $wpdb->prepare() method for sanitation. Are you looking for something like this? function combined_downloads($post_id) { global $wpdb; return $wpdb->get_var($wpdb->prepare( "SELECT SUM( meta_value ) FROM $wpdb->postmeta AS m LEFT JOIN ...


0

First you should make some improvements to your template file. Try this: <?php get_header(); ?> <?php global $woo_options; ?> <?php if ( $woo_options['woo_featured_disable'] <> "true" ) include( TEMPLATEPATH . '/includes/featured.php'); ?> <?php $args = array( 'post_type' => 'infobox', 'order' => 'DESC', // DESC for ...


0

Atlast I found a way add_action( 'init', 'run_this_code', 1 ); function run_this_code() { ob_start(); require('dynamic-css.php'); $content = ob_get_contents(); ob_end_clean(); $f = fopen("custom.css", "w"); fwrite($f, $content); fclose($f); }


1

Lets start with your first code. If you add texts, captions etc. a shortcode will be generated: [caption] The function img_caption_shortcode() in wp-content/media.php is responsible to execute the shortcode. But this shortcode is only produced, when you use captions. Otherwise, it will be simply inserted the <a>- and <img>-Tag. who decides, what ...


0

look a the 'Using a Hook' on here: WP Codex


0

Just use the $product parameter to get the custom field value, and determine what to return: function custom_price_html( $price, $product ) { if ( ( int ) get_post_meta( $product->id, 'price_per_person', true ) ) $price .= ' per person'; elseif ( ( int ) get_post_meta( $product->id, 'price_per_group', true ) ) $price .= ' per ...


0

Could you go to this page for us: ../wp-admin/options-permalink.php What is the structure chosen? That will give you the answer for how to adjust the function on the bottom edit: Also, Why is this part like this: if ($echo) { echo $output; } return $output; } Shouldn't $output be in parenthesis?


0

The wp_get_archives function in Wordpress does not support displaying dates. However my solution worked without using this function it's still probably handy for your or someone else his cause: In the functions.php file of your theme place the following: function recentPostsDate() { $rPosts = new WP_Query(); $rPosts->query('showposts=6'); ...


1

get_template_directory() gives you the path to the parent theme while get_stylesheet_directory() gives you the path to the child theme. Docs: https://codex.wordpress.org/Function_Reference/get_stylesheet_directory https://codex.wordpress.org/Function_Reference/get_template_directory


1

You should hook save_post instead of publish_post. publish_post only runs when a post is initially published and won't catch subsequent saves. Additionally, the publish_post hook passes parameters to your function and those should be used to retrieve info about the post being published rather get_the_content(), which only works when you're inside the ...


0

Please Use this For custom post type after register_post_type add_theme_support( 'post-thumbnails', array( 'post', 'movie' ) ); // Posts and Movies OR $args = array( 'labels' => 'Basics', 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, ...


0

If you use update_post_meta, a meta field will be added to the post with the specified key. If the key exists, the meta field will be updated. If there are several meta fields with the same key, all of them will be updated. To add repeating fields, you must use add_post_meta with the third parameter set to false (it is already false as default). This way, ...


0

Change this code add_action( 'after_setup_theme', 'right_sidebar_widget' ); with add_action( 'widgets_init', 'right_sidebar_widget' );


0

Since you're using the same key for all the values you can simply use: update_post_meta($this->current_post_id, 'assigned-sales', $response->records); This will store the whole array in a single meta entry with key of 'assigned-sales'. WordPress will update the value if it exists and create new one otherwise, no need to have logic for that. In ...


1

I haven't looked in detail at your code, but what strikes me is your open_csv method. You have the following lines return $attributes['file']; return $attributes['type']; As your method currently stands, it stops and returns the value of file right after setting the attributes. Here is what return does in php If called from within a function, theĀ ...


0

I'm not sure what your exact approach here is, but the above can be converted easily into a function. I have commented the code so you can follow it. (Requires PHP 5.4 as it uses the short array syntax. Also, I have not included markup, you need to add it as needed) function category_post_list() { /* * Stop function and return null this function is ...


0

The solution to your problem can be accomplished with a page template, but first we need to fix your previews code <?php /**/ function add_user_page($user_id){ $user = get_userdata($user_id); $role_with_page = "subscriber"; if( reset($user->roles) != $role_with_page ) return; //Exit if user role doesn't allows ownership of page ...


0

The first example is correct. Only if you use a child theme you need to use get_stylesheet_directory_uri() instead of get_template_directory_uri(). I assume there is another problem elsewhere. You should check your PHP error log.


0

Try changing wp_enqueue_script() to wp_register_script() and then after that add wp_enqueue_script('wowjs');


0

hackish solution: since wordpress add a lot of classes by default to the <img>, if you don't change that behaviour by force, you can always 'inject' something via str_replace before the string class=. In code: $image = get_the_post_thumbnail( $post->ID, 'medium', array( 'class' => 'myclass' ) ); $moreattrs = 'data-fullimg= "full.jpg"'; $image ...


0

get_the_terms() will return a boolean false under some circumstances: A post with no terms assigned gives a false result, not an empty array. https://codex.wordpress.org/Function_Reference/get_the_terms#Returns It sounds like that is what is happening. You need to check that $post_link = to ensure that it is the type you expect before trying to ...


2

You can use get_pages(); or get_posts(); or WP_Query to get a list of pages. They differ somewhat in details, so read up on that. But generally all of them should suffice, for you to achieve your goal. Just choose the best fitting one for your purpose. For getting pages by name or better title you can use: get_page_by_title() Or use: ...


0

wc_enqueue_js is not method of $woocommerce object. Just call the function. wc_enqueue_js( file_get_contents( WOO_CK_PATH . 'panel.js' ) );


0

Okay, so found the solution (sorry for the delay) I had to put this: // Add Shortcode function city_name() { global $current_cityinfo; return ' '.$current_cityinfo['cityname']; } add_shortcode( 'city name', 'city_name' );


3

You can use PHP get method to process these variables. <?php if( $_GET['action'] == 'discussion' ) { ?> It's True. <?php } else { ?> It's False. <?php { ?>


1

If that is the sum of the code for that file then the only place near the bottom is that do_action-- do_action( 'shiroi_after_post_body_content' );-- which looks to be what you want. function test_new_action() { echo 'Howdy'; } add_action('shiroi_after_post_body_content','test_new_action'); Depending upon how this file is included there might be other ...


0

add_action( 'admin_footer', 'my_action_javascript' ); // Write our JS below here function my_action_javascript() global $wpdb; $pageposts = $wpdb->get_var("SELECT max(menu_order) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type ='post_trainers'"); ?> <script type="text/javascript" > ...


0

This works better for me overriding both blog posts and static page in Settings > Reading > Front page displays: <?php /** * Set custom post type archive as front page. * * @since 1.0.0 */ function ql_set_as_front_page( $query ) { if ( is_admin() || ! $query->is_main_query() ) { return; } if ( ql_is_front_page( $query ) ) { ...


0

This is more of a pure PHP than WordPress. You are implicitly creating $ranNum as local variable in function scope. $Example_Widget won't access widget instance in PHP. What you should be using is $this->ranNum, for reading and writing. That will create object's property implicitly, but you likely should declare it explicitly in your class definition ...


1

You have to use single_product_large_thumbnail_size filter for Single Product page. something like: function custom_product_large_thumbnail_size() { return 'gallery'; } add_filter('single_product_large_thumbnail_size', 'custom_product_large_thumbnail_size');


-1

You have to regenerate all the images with the specified sizes and for that you can use Regenerate Thumbnails plugin from wordpress. you can download it from http://wordpress.org/plugins/regenerate-thumbnails/


0

You should be passing the 'cityname' to your function: function get_city_name($cityname) { return $cityname; } add_shortcode( 'city-name', 'get_city_name' ); Your shortcode would then look like: [city-name cityname="London"] chances are that your function will return an array, but this should help you move forward


1

You need to get $current_cityinfo first. As in: $current_cityinfo = get_option('current_cityinfo'); Or add this code: global $current_cityinfo; above your return line.


0

I am using this plugin: https://wordpress.org/plugins/font-awesome-4-menus/ which does exactly what you need. Go to your Screen Options panel (top right) and activate CSS Classes. Then add fa-home fa-fw to your CSS class field.


1

The problem is that wp_reset_postdata attempts to restore $post from the main $wp_query, but on post.php in admin, $post isn't populated from $wp_query, so wp_reset_postdata fails to restore it. The solution is to use get_posts and a foreach instead of WP_Query and the loop.


2

the_widget() is used to display a specific widget outside of a sidebar. To be honest, this function serves no valuable purpose as you need to hard code the widget $instance parameters, this means that you will need to manually change the code every time you need to change something. So much for dynamica. It would just be easier to simply code a custom ...


5

Via using the dynamic_sidebar() you can change/edit the widgets in the widgets area of the appearance settings. If you use the_widget() you hard code it straight to template so you need to modify the code to change what is displayed. Both have their uses. Too many sidebars tend to clutter the widgets area, but is very good for users that don't want to mess ...


0

There are two problems that I can see that cause your code to fail. The first is that you're using update_page_meta, which does not exist. Use update_post_meta instead. The second is that you're using the action save_page, which also does not exist. Use save_post, save_post is an action triggered whenever a post or page is created or updated, so it is ...


-1

try this way: add_action('save_post', 'wpse_181902', 10, 2); function wpse_181902($id, $item) { if ($item->post_type == 'page') { //Your custom logic update_page_meta( $id, 'bookinfo', $booktxt); } }


1

You have a problem with your code formatting and syntax at the very least: function ref_access(){ global $error; if (is_user_logged_in()) // <-- problem here... $newdb = new wpdb( 'user', 'pass', 'db', 'localhost' ); global $newdb; $hf_username = wp_get_current_user(); $inputValue = $_POST[$quanid]; $wpdb->insert( $table, array( 'ItemID' => ...



Top 50 recent answers are included