New answers tagged

1

Can you show full code in your unit tests bootstrap file? I am guessing the plugin would not be loaded properly, here is how it can be loaded as a mu-plugin when running tests. Anyway, I would suggest you to use WP CLI's command wp scaffold plugin-tests to get started with tests quickly documented here.


-1

Get all woocommerce product and its details like sku,price,product name $sql = "SELECT * FROM `wp_posts` wp LEFT JOIN wp_postmeta wpm ON wp.ID=wpm.post_id WHERE post_type='product'"; $results = $wpdb->get_results($sql); print_r($results);


0

Since wordpress 4.5 you can add inline script by wp_add_inline_script(). To add a javascript snippet to the footer that requires jQuery, this code helps you function enqueue_jquery_in_footer( &$scripts ) { if ( ! is_admin() ) $scripts->add_data( 'jquery', 'group', 1 ); } add_action( 'wp_default_scripts', 'enqueue_jquery_in_footer' ); ...


0

Change the_field() to get_field() the_field() = echo get_field()


0

Most likely this is coming from the Theme's language file, can you explain why the translating has to be done by the plugin? Language files are designed for this. But, if you really want to do this, use the gettext filter: https://codex.wordpress.org/Plugin_API/Filter_Reference/gettext Using this to much may impact preformance. https://pippinsplugins.com/...


1

When you are concatenating string you shouldn't use ;after the method. Also you should close que quotes, call the method, and open again. Try the code below: $listItem .= '<div id="agenda-block-image-responsive" style="' .get_field('speaker-img' ) . '">';


0

The Codex says: term_id is the ID of a term in the terms table term_taxonomy_id is a unique ID for the term+taxonomy pair What does this mean? A term is a word. It can belong to a taxonomy, such as tags, categories, or a custom taxonomy. The thing is: there can be several taxonomies containing the same term. Let’s say you have a term called “fattening“....


0

I would suggest googling "wordpress ajax tutorials". Here is one that was beneficial to me. https://premium.wpmudev.org/blog/using-ajax-with-wordpress/ The first problem I see is you have not stated what your 'action' variable is. The JavaScript AJAX call specifies what function is targeted in the 'action' attribute. In WP, this is the hook executes your ...


0

As this is a part of a bigger understanding of the design I will describe it as whole... :) In WP 4.5.3 there are still all these tables (I will talk about them without prefix): posts term_relationships term_taxonomy terms The path to get post terms' readable names goes through them all. posts the main identificator here is ID - an id of a post (of any ...


0

get_post_permalink won't work for draft or pending posts. But there are a couple workarounds you could use. Here there is a similar post with two options.


3

I think it's impossible to give a definitive answer here, because choiches like this are personal preference. Consider that what follows is my approach, and I have no presumption it is the right one. What I can say for sure is that you should avoid your third option: Just return null/false This is bad under different aspect: return type ...


1

You can't without hooking a filter to recognise the files in your plugin BUT you can hook into the template_include filter and register your own file e.g. add_filter('template_include', 'my_function_name'); function my_function_name( $template ) { if( is_post_type_archive( 'post_type' ) ){ $template = dirname( __FILE__ ) . '/templates/archive-post_type....


1

This is stored in the user meta table under the rich_editing meta key for each user. You could one of these (untested): 1) Add it for your specific users, e.g. via `add_user_meta( $user_id, 'rich_editing', 'true', true );` 2) Within the wp_default_editor() function, that determines the default editor, the user_can_richedit() function is applied. It ...


-1

Have you enabled debugging in wordpress? Open wp-config.php in the root wordpress folder and change this line: define('WP_DEBUG', false); to define('WP_DEBUG', true); Your PHP errors will then output to the screen.


0

Check out the hook theme_page_templates, used by the get_page_templates function, filter added since 3.9 updated in 4.4: apply_filters( 'theme_page_templates', $page_templates, $this, $post );


0

Just change both lines: wp_register_script( 'wildstyle', plugins_url('css/wildstyle.css', __FILE__ ) ); wp_enqueue_script( 'wildstyle' ); to wp_register_style( 'wildstyle', plugins_url('css/wildstyle.css', __FILE__ ) ); wp_enqueue_style( 'wildstyle' );


0

Unit testing plugin functions in Wordpress is extremely difficult. It's usually easier to do an acceptance or functional test. The difference is that in a unit test, you have to mock everything you interact with. In a functional test, you can run the whole system. For instance, I use Codeception to launch a browser and test against the whole system. Opinions ...


2

You can add stylesheet in back-office, but you need for that use the admin_enqueue_scripts action. See Codex reference : https://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts


0

You can also use add_meta_box to add predefined boxes to different places in the admin, as long as you know the right handles. In the case of the comments meta box you need: add_meta_box('commentsdiv', __('Comments'), 'post_comment_meta_box', $screen, 'normal'); Here post_comments_meta_box is the callback function that fills the metabox. I am supposing ...


1

There are scripts available which can do the job for you. As per my understanding, you will have to host your plugin updates on a remote location so that the update checker can access that and do the necessary job. You can take a look at this script which seems to be very useful as per the comments. You can also find examples on that page which you can ...


2

In the template part used in the loop on your home page, something like this should get you started. Someplace after the_content(); or the_excerpt(), depending on what you're using. <?php if ( is_home() ) { ?> <a class="button" href="<?php echo esc_url( get_permalink() );?>#comments"> Comment Link Text </a> } ?> ...


1

Please do not put this code into production OK. So there are many things wrong with the code that need fixing before we can even begin to answer your question. Firstly we're using $_REQUEST without good reason -- $_REQUEST gives us access to all of the information sent in the request, which means that $_POST['foo'] $_REQUEST['foo'] === $_GET['foo'] and ...


0

I can't comment on the validity of the AJAX call; it's a different style than I have used -- but inside the click(function(e) { code, you might try trapping the default page action to prevent it from triggering the page refresh: $("#myelement).click(function(e){ e.preventDefault();


-1

The solution I read nowhere on the internet: tb_position is the thickbox function that do strange things with the window size. You can easily and simply overide it by the time thickbox call it. old_tb_position = tb_position; tb_position = function(){ $("#TB_window").css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: TB_WIDTH + 'px', ...


1

If you hook into the hooks provided in update_post_meta() function and still use this function in your callback, that would result in a loop which would probably end with the timeout.. As the per the comments: add_action("wp_insert_post", function( $post_ID ) { if ( ! $post_data = get_post( $post_ID ) ) return; // try to make this run only once ...


0

There is a lot of nuance here, depending on what exactly you want to accomplish. I don't feel confident I completely grasp it from your question. In general WP logic flow is following: URL is converted to query vars query vars go into main query instance main query instance queries posts template loader uses conditional tags (which use main query) to ...


6

It depends on how you are going to use the stored data. If you want to run complex queries against the values, use a custom table with indexes optimized for those queries. If you will always just fetch all the values for a given object, create a non-public custom post type, and store the data as post meta. Do not store the data in a serialized string, ...


4

Where to store plugin settings fields? Options table FTW. It's cached and easy to do CRUD. Settings API or Options API? Basically, you can use Options API without Settings API but you cannot use Settings API without Options API. Even when you just need to add some fields to a existing WordPress page, you still need get_option() to retrieve data for ...


1

Yes this is actually the same as point 1, just without the helpers Now this depends on how you want to use your setting. The instinct is that at 99% of the cases this will just add unneeded complexity to your code and hurt performance. As long as we are talking about setting and not content or widgets, the settings API is what you should use. It takes ...


0

You have to use $_REQUEST instead of $_POST in PHP: function rnr_contact_callback() { $name = $_REQUEST['firstName'] . ' ' . $_REQUEST['lastName']; wp_mail( 'myname@gmail.com', 'Contact form submitted', $name . '(' . $_REQUEST['email'] . ') sent a message: ' . $_POST['comment'] ); exit; }


2

There seems to be two separate question here: Should one do this? Not under most circumstances. Modifying third party extensions de-facto forks it with all implications of future maintenance and/or merging upstream (from original) changes. Without long term commitment it's just not practical and promptly falls apart, like in the cases you witnessed. ...


1

If the original plugin has some form of GPL license, it is okay to manipulate it as long as you honour the license. So, there is nothing legally wrong with editing a plugin, a theme or even WP core. However, if you do this, you take over responsibility for updates from the original builders. In a way, you have become the author of a new plugin. If you don't ...


0

The testing environment generated by WP-CLI is designed to unit test a single plugin, not for integration testing as pointed out by Mark Kaplun. However, by following this post on Unit Testing Themes and Plugins in WordPress and you can load themes and additional plugins to do broader intergration tests.


1

Including a PHP file requires to provide its exact file path, not the URL. Try this instead: include_once( WP_PLUGIN_DIR . '/plugin-name/classes/event.php' ); Hope it helps.


0

You need the generate_filename method of the image editor class. Like this: $filename = $img->generate_filename( 'resized', ABSPATH.'wp-content/uploads/resized-images/'); $image->save($filename); This will save the image, renamed originalname-resized, in the resized-images directory of the upload folder. This does not mean WordPress knows it is ...


2

Using default page template has the same issues than using single post template. It is true that usually page tempaltes has not post information like author, date and so on, but it is totally up to the theme developer. Even there are themes without page templates. But if you want to force the use of the default page template for your custom post, you can do ...


0

Pagination is result of combining two factors: page size and offset. In common case page size is constant and offset is page size times current page. Is it possible to build highly elaborate pagination you describe? Technically yes. That would be matter to adjusting page size and offset in a more elaborate fashion. However specific of such implementation ...


1

What you are trying to do is not unit testing but integration testing, and as integration usually requires some initialization which will be specific to the relevant plugin it is hard to give a general answer which is more then the obvious"include the plugin file" The idea of unit testing is to check that your APIs do what they are supposed to do and the ...


0

Well once I looked into it, it is quite simple. $_POST is global variable and contains all the POSTed data that was sent from the form. For example, to email yourself all the posted data submitted in the form, you you could use something like this: function email_me_post_data() { global $_POST; $msg = print_r($_POST, true); mail(...


1

You can use the register_post hook. Here is the list of hooks and filters that fire during user reg (in order that they are fired): register_post - used to handle post data from a user registration before the registration_errors filter is called or errors are returned registration_errors - filters the errors encountered when a new user is being ...


0

@SamuelEth is right, creating a page to handle this (with rewrites) could be the way to go. Another alternative would be registering a custom post type of my-plugin(with the admin UI setting off since you don't want user access to it)... ...and dynamically detecting and handling calls to that, the advantage being the rewrite is done for you somewhat. ...


0

Note what happens when you provide sanitize callback in register_setting(). It register a filter to sanitize your options add_filter( "sanitize_option_{$option_name}", $sanitize_callback ); Now when you do update_option() then trigger your own function to prevent saving :D Because update_option() calls $value = sanitize_option( $option, $value ); ...


0

This is just a hunch, but it seems to me that this problem has something to do with the order in which the operations are called. If you have defined your hooks wp_ajax_remove_installing_data and wp_ajax_nopriv_remove_installing_data in your plugin and hooked that plugin in the usual way into WP with the init hook, it means this code is executed before ...


0

If you go to another page with link you can add parameter to the url and used GET to get the parameters value: Your link: www.example.com/?par1=mypar1_value&par1=mypar3_value&par3=mypar3_value In the page you can get the parameter and values like this: <?php $par1 = $_GET["par1"]; $par2 = $_GET["par2"]; $par3 = $_GET["par3"]; ?>...


2

This is why you need to add rewrite rules on the init action on every request, as well as on plugin activation. When your plugin deactivation hook is run, other plugin init hooks have already run, so their rules exist in the global variable that stores them for the life of each request. Flushing rewrite rules empties the option that stores the rules long-...


2

1. Add a new rewrite rule: add_action('init', function() { add_rewrite_rule('^dog/([^/]+)/?$', 'index.php?cat=dog&name=$matches[1]', 'top'); }, 10, 0); 2. Filter the post link: add_filter('post_link', function($post_link, $post, $leave_name = false, $sample = false) { if ( is_object_in_term($post->ID, 'category', 'DOG') ) { $...


0

Thanks for the reply. The oscommer_importer.php file already tried to retrieve fields from products table. Here I have attached it. ` <?php /* Plugin Name: OSCommerce Product Display Plugin URI: http://localhost:8383 Description: Plugin for displaying products from an OSCommerce shopping cart database Author: Priya Mohan Version:...


0

The fact that the plugin is installed and activated and has it's PHP code executed doesn't mean that it's suddenly part of WordPress. You should let your plugin initialise on a WordPress hook, meaning that the code will not be executed untill WordPresshas reached a certain stage in the execution process. You're actually already doing a lot of these hooks so ...


0

I'm not sure if you're trying to use WordPress functions outside of the actual WordPress application, but to accomplish this you simply need to do: require( 'path-to-wordpress/wp-load.php' ); at the top of your file, if the path to the WordPress application is in fact $_SERVER['DOCUMENT_ROOT'] then you would do require ( $_SERVER['DOCUMENT_ROOT'] . '/...


0

I think the problem might could be the logic with using checked... <input type="checkbox" name="aps_display_post_title" value="yes" <?php if ($aps_display_post_title != 'no') {echo 'checked';} ?> /> ...on the other hand then running sanitize_text_field on the checkbox value might also be causing the problem. Instead you might want to break ...



Top 50 recent answers are included