Skip Navigation

[Resolved] Custom code snippet from localhost isn't working when I put it on a live server

This support ticket is created 5 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by matthewC-9 5 years, 3 months ago.

Assisted by: Beda.

Author
Posts
#1308317

I have a custom code snippet that's intended to exclude posts from a view if a start date hasn't happened yet or an end date is past, while showing them if those dates are empty. Here's the snippet, in case it makes a difference:

<?php
/**
 * New custom code snippet (replace this with snippet description).
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

add_filter( 'wpv_filter_query', 'dcl_check_visible_dates', 99, 3 );
function dcl_check_visible_dates( $query_args, $view_settings, $views_id ) {
  if ( $views_id == 126 ) {
    $query_args['meta_query'][] = array(
      'relation' => 'OR',
      array(
              'key'     => 'wpcf-end-date', 
              'compare' => 'NOT EXISTS',
        ),
      array(
              'key'     => 'wpcf-end-date', 
              'value'   => current_time('timestamp'),
              'compare' => '>',
        ),
    );
    $query_args['meta_query'][] = array(
      'relation' => 'OR',
      array(
              'key'     => 'wpcf-start-date', 
              'compare' => 'NOT EXISTS',
        ),
      array(
              'key'     => 'wpcf-start-date', 
              'value'   => current_time('timestamp'),
              'compare' => '<=',
        ),
    );
  }
  return $query_args;
}

On my localhost, this works great. When I push everything to hidden link, it doesn't (everything else is the same otherwise). The yellow band there is a post that shouldn't be showing up because its end date is past.

I did notice that after doing a database push from local to staging, the snippet name changes and it's set to inactive (so it doesn't match localhost like I'd expect), but changing the name back and activating it doesn't make it work. I also tried recreating the snippet from scratch on staging, still with no luck. What else can I try to troubleshoot this?

#1308561

Most likely $views_id == 126 is not correct?

If that isn't the cause, I can only suggest to debug this like any other code issue:

1. In the place you use this code (whether functions.php or any snippets location), insert another code, simple echo or so, to test if code is triggered at all in that place
2. If yes, you know the code must work, and hence you can start checking at what point it fails by either echoing something or var_dump() at the given lines of code before and after conditions.

You'll quickly narrow down where in the code itself is not triggered anymore or fails, and hence can fix that.

The only I can imagine so far is that on your live site the ID of the View is not the same as locally.

#1309769

Hey Beda - thanks for the reply. After testing this some more, I realized that the snippet was not actually active, and it never activates on my first try after making a database push. This isn't a huge deal, but it does seem like a little bit of a glitch. My steps are:
1. Push local database over to staging.
2. Log in to staging and go to Toolset > Settings > Custom Code.
3. Click Edit.
4. Change dcl_check_visible_dates.php-2 back to dcl_check_visible_dates.php.
5. Hit Save and Close.
6. In the Custom Code dashboard, hit Activate, get success message, see Status column change to active (but visiting test page shows snippet isn't working).
7. Refresh Toolset > Settings and click back over to Custom Code, which now shows status = inactive.
8. Click Activate, get success message, see Status column change to active.
At this point the status change has actually stuck and the snippet is working.

So that's one thing, that the snippet won't activate the first try despite the success message. The other thing that's weird is that it needs to be activated in the first place. The database I'm pushing over is an exact copy, and the dcl_check_visible_dates.php snippet is also an exact copy (managed by Git) - when I look at them individually, they look just like what's on localhost - so it's weird that for some reason it's showing up in the settings as dcl_check_visible_dates.php-2 & inactive in the first place. Any ideas about that?

Now that I know what's happening, neither of these things are a huge deal, but it would be nice to get them working so I don't have this extra workaround in my workflow.

#1310283

When I use Duplicator or copies of Databases, this does not happen.

Maybe it's related to the export/import/push process.

Can you replicate the same issue with a simple Duplicate or Copy of the Database deployed over another site?
If you can, could you send me that database or set up so I can try to see what's happening?

I deploy users sites daily several times, this is why it is surprising hearing of this issue:
I never saw it happening yet. But there is always a first 🙂

Looking forward to hear from you, and to get to the ground of the issue...

#1311815

Not able to replicate with Duplicator, so I'm closing this for now.