thehazels - websites made simple thehazels - websites made simplethehazels | websites made simple

Ajax queries are redirected (admin-ajax.php 302)

I’ve just spent a lot of time working out why an ajax function was not working for non-logged in users.

I’d added an action add_action('wp_ajax_nopriv_my_function', 'my_function') and I’d made sure admin-ajax.php was localized wp_localize_script('ajax-post', 'wp-ajax', array('ajaxurl' => admin_url('admin-ajax.php')).

Unfortunately the call was made, but never returned. The reason was because admin-ajax.php was being redirected to the home_url. This had been done in one of my plugins. Unfortunately they’d used a sledgehammer approach to redirect all non-logged in users away from admin pages to the home page. I found a better approach for the redirection in WordPress StackExchange:

function my_admin_init(){
  if( !defined('DOING_AJAX') && !current_user_can('manage_options') ){
    wp_redirect( home_url() );
    exit();
  }
}
add_action('admin_init','my_admin_init');

19. January 2015 by Antonia Whitaker
Categories: Wordpress | Tags: | Leave a comment

PHP .user.ini

Just discovered a great new feature of PHP 5.3. .user.ini can override the main php.ini directives. Anything not classified as PHP_INI_SYSTEM in the List of php.ini directives can be set here and used locally.

I’ve found it very useful for error reporting. On my development sites, I want to be able to see all errors immediately – including those at NOTICE level which can get forgotten in a log file. However, this is not acceptable on a production site and my web hosting company, quite rightly, has the main php.ini display levels set to off, with all errors logged to the error log. So all I have to do in .user.ini is:

display_errors = On
display_startup_errors = On

I did think about setting them using the PHP function error_reporting(), however, this would still show the ‘white screen of death’ since the PHP script would have failed before error_reporting() was run.

14. November 2014 by Antonia Whitaker
Categories: Tools | Tags: | Leave a comment

Identifying radio buttons

On document ready, to find the value of the check radio button, use the id together with  ‘checked’. E.g.:

$('#myradiobutton:checked').val();/>

However, to trigger a click function on the radio button, id wasn’t recognised, but the input name was. E.g.:

$('input[name=myradiobutton]').click(function{});

Perhaps it’s better to use the input name, rather than the id.

01. May 2014 by Antonia Whitaker
Categories: Code | Tags: | Leave a comment

Horizontal rule
: css styling

I’ve just realised how adaptable the <hr> (horizonal rule) element can be by using css styling. I’ve never much like the default look and so have often changed it to a simple single line. This isn’t as simple as it sounds since an <hr> is an element composed of padding, surrounded by borders, surrounded by margin. So a single black line:


requires:

hr {
   padding: 0;
   border: none;
   border-top: 1px solid black;
}

Using the margins, you can make it shorter and centred:


hr {
   padding: 0;
   border: none;
   border-top: 1px solid black;
   margin: 0 5em;
}

or just indent it one side:


hr {
   padding: 0;
   border: none;
   border-top: 1px solid black;
   margin-right: 10em;
}

but the best is playing with colours:


hr {
   padding-top: 5px;
   background-color: red;
   border: none;
   border-top: 5px solid blue;
   border-bottom: 5px solid yellow;
   margin: 1em 20em;
}

hr {
   padding-top: 40px;
   background-color: red;
   border: none;
   border-left: 40px solid blue;
   border-right: 40px solid yellow;
   margin: 1em 120px;
   width: 120px;
}

28. March 2014 by Antonia Whitaker
Categories: Tools | Tags: | Leave a comment

Doctrine query builder in Symfony 2

I’ve found many different opinions on the best way to use Doctrine query builder in Symfony 2. I needed to return a query joining four tables. I hit many errors before finding a working solution.

The requirement was to use a custom query to build a select list in a form. Symfony’s documentation recommends using the repository for this.

First thing to bear in the mind is that most repository functions return a result generated with createQuery. This time I needed a function returning a query builder object.

I also had two parameters to pass. These can be passed to the form in the $options array (remembering to add them to  the form’s setDefaults). My setParameters were ignored until I applyed them to the where clauses.

The most difficult error was a ‘500 End of script output’. The query needs to return the entity object, and I has only specified the field.

The form:

public function buildForm(FormBuilderInterface $builder, array $options)
{ 
      $customer = $options['customer']; 
      $type = $options['type'];
      $builder
        ->add('description', 'entity', array(
            'class' => 'MyBundle:TableA',
            'query_builder' => function( EntityRepository $er ) use ( $customer, $type ) {
               return $er->findCatTypesQuery( $customer, $type );
            },
....
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'MyBundle\Entity\TableA',
        'customer' => 1,
        'type' => '',
    ));
}

TableA’s repository:

$em = $this->getEntityManager();
$query = $em->createQueryBuilder( )
     ->select(a)
     ->from( 'MyBundle:TableA', a')
     ->innerJoin('b.item', 'b')
     ->innerJoin('c.item', 'c')
     ->innerJoin('d.item', 'd')
     ->where('a.type = :type')->setParameter('type', $type)
     ->andWhere('b.customer = :customer')->setParameter('customer', $customer)
     ->orderBy('a.description')
     ;
return $query;

26. March 2014 by Antonia Whitaker
Categories: Symfony | Tags: | Leave a comment

Backups

I’ve been looking at various backup plugins to try and see how reliable it would be to set up an automated backup. As ever the main requirement is free of charge since I can’t pass on the cost. There seem to be two options.

Either backup to the server and then upload it somewhere else using cron:

  • Simple Backup By MyWebsiteAdvisor – looked promising but seems to kill mysql server periodically. I also lost all my revisions since I ticked the options to delete all revisions and temporary options – I thought it was excluding them from the backup, but it actually deleted them from the database. Also I’d have to set up the cron job myself and it only backups to the server which can causes space issues if not monitored.

or backup to Dropbox or similar, using wordpress scheduler:

  1. WordPress Backup to Dropbox by Michael De Wildt. Installation was invasive – new menu item, rather than adding to Settings and four new database tables – including one called options which could easily have been a single row in wp_options. Although no errors were reported, no backed was done either
  2. Ready! Backup By Backup Plugin Ready. 6 database tables and only ftp option available as free. Nice interface and did the backup … finally found it, but can’t schedule
  3. BackWPup Free – WordPress Backup Plugin – more tables. Didn’t work, but at least gave an error: The HTTP response test get an error “Failure when receiving data from the peer”.
  4. UpdraftPlus – WordPress Backup and Restoration By Backup with UpdraftPlus. Excellent documentation. Dropbox worked, but not scheduler (even though it works on wordpress crons)
  5. BackWPup Free – WordPress Backup Plugin By Inpsyde GmbH. Very flexible but I couldn’t connect to Dropbox.

If I was able to pay, I’d probably try out on of the following. They are well maintained and have a lot of positive reviews:

  • myRepono WordPress Backup Plugin
  • blogVault Real-Time WordPress Backup

Conclusion

Finally understand my scheduling problem, even if I haven’t worked out how to fix it. My web hoster has disabled loopback connections to the same host because I’m on a shared server (hence the curl error ‘Failure when receiving data from the peer’).  ALTERNATE_WP_CRON is meant to try and get around this, but it hasn’t. Unfortunately I can’t spend any further time on this, but I am puzzled since the WordPress update checks are working fine; as does Akismet.

I found some useful information on the way:

If I do get the scheduling issue sorted, I think Updraft Plus was the closest to what I want.

 

12. March 2014 by Antonia Whitaker
Categories: Wordpress | Tags: | Leave a comment

Testing google maps

02. March 2014 by Antonia Whitaker
Categories: Wordpress | Leave a comment

My results of trying out various calendar plugins

I wanted:

  • no charge (for a club website)
  • ajax event view
  • recurring events with a means of deleting or ignoring specific dates (e.g. for a bank holiday)
  • simple to add a new event
  • link to a map – would be nice to be able to save locations
  • works with WordPress 3.8

Ajax Event Calendar

Very nice calendar and interface. However it is only compatible up to 3.5 and is no longer maintained. Unfortunately there are jquery problems that were breaking other plugins and it would have been too big a problem to try and fix.

The Events Calendar

Compatible up to 3.8. Have to pay for recurring events. $65 a year is too much for recording non revenue making events.

All-in-One Event Calendar

Compatible up to 3.7. Quite an invasive installation which I tried twice. The second time I no longer had sufficient permissions to edit the settings.

CalPress Event Calendar

It’s compatible up to 3.6 and there were a few hiccups – 500 error when first adding a new event, sorted once I’d saved the settings – an ajax spinner continuing after it should have stopped to report a missing item before event could be saved. However, I found the actual calendar a bit complicated and too themed – pretty but not in line with rest of page.

Events Manager

Compatible up to 3.8. very comprehensive, but too complicated for what I want.

WordPress Event Calendar (aka Spider Event Calendar

3.7 compatible. Installation generated the warning: The plugin generated 236 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin. Slightly annoying interface and too much emphasis on trying to sell you extra features.

Event Organiser

Compatible up to 3.6. Very nice. Only error so far is minor – though jquery clashed with another plugin.

Notice: wp_add_inline_style was called incorrectly. Do not pass style tags to wp_add_inline_style(). Please see Debugging in WordPress for more information. (This message was added in version 3.7.)

Think this is the one.

Events Made Easy

Compatible up to 3.8. Nice admin – integrates well with good layout. Unfortunately not quite what I was looking for.

Booking Calendar

Compatible up to 3.8. Probably good for bookings, but doesn’t handle events.

Notice: Undefined index: booking_type in /home/sites/thehazels.org.uk/public_html/wp-content/plugins/booking/lib/wpdev-bk-timeline.php on line 872

 

21. January 2014 by Antonia Whitaker
Categories: Wordpress | Tags: | Leave a comment

wp_enqueue_script warning

Notice: wp_enqueue_script was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging in WordPress for more information. (This message was added in version 3.3.)

This PHP notice warning appears if WP_DEBUG has been set to true. It will have been caused by a theme or plugin where a script has not been correctly enqueued. WordPress Codex page WP_ENQUEUE_SCRIPT gives examples of how it should be used.

07. January 2014 by Antonia Whitaker
Categories: Wordpress | Tags: , | Leave a comment

remove_action function

There are occasions using a child theme when you want to replace the original action with your own. WordPress provides a function for this:

 <?php remove_action( $tag, $function_to_remove, $priority, $accepted_args ); ?> 

However, if you are overriding a parent theme, then the action will not yet exist. I found this useful filter on WordPress Answers.

add_filter( 'the_content', 'unhook_custom_function', 11 );
function unhook_custom_function( $content ) {
  remove_action( 'some_hook', 'custom_function' );
  return $content;
}

11. December 2013 by Antonia Whitaker
Categories: Wordpress | Tags: | Leave a comment

← Older posts