DEV Community

loading...

PHP error (Illegal string offset)

Manuel Alonge
・2 min read

Hi All,

I am working on a wordpress website with the Advanced Custom Fields plugin and in relation to the code below (functions.php) I receive this error message on the Front End side of the website. Could you please give me some help?

Warning: Illegal string offset 'sizes' in C:\wamp6456\www\fictional-university\wp-content\themes\fictional-university-theme\functions.php on line 15

Warning: Illegal string offset 'pageBanner' in C:\wamp6456\www\fictional-university\wp-content\themes\fictional-university-theme\functions.php on line 15

Code (functions.php)

<?php

function pageBanner($args = NULL) {

  if (!$args['title']) {
    $args['title'] = get_the_title();
  }

  if (!$args['subtitle']) {
    $args['subtitle'] = get_field('page_banner_subtitle');
  }

  if (!$args['photo']) {
    if (get_field('page_banner_background_image')) {
      $args['photo'] = get_field('page_banner_background_image')['sizes']['pageBanner'];
    } else {
      $args['photo'] = get_theme_file_uri('/images/ocean.jpg');
    }
  }

  ?>
  <div class="page-banner">
    <div class="page-banner__bg-image" style="background-image: url(<?php echo $args['photo']; ?>);"></div>
    <div class="page-banner__content container container--narrow">
      <h1 class="page-banner__title"><?php echo $args['title'] ?></h1>
      <div class="page-banner__intro">
        <p><?php echo $args['subtitle']; ?></p>
      </div>
    </div>  
  </div>
<?php }

function university_files() {
  wp_enqueue_script('googleMap', '//maps.googleapis.com/maps/api/js?key=AIzaSyBh9b1rNCp6kOi5JeMHiRP4klDymBeoEWk', NULL, '1.0', true);
  wp_enqueue_script('main-university-js', get_theme_file_uri('/js/scripts-bundled.js'), NULL, '1.0', true);
  wp_enqueue_style('custom-google-fonts', '//fonts.googleapis.com/css?family=Roboto+Condensed:300,300i,400,400i,700,700i|Roboto:100,300,400,400i,700,700i');
  wp_enqueue_style('font-awesome', '//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css');
  wp_enqueue_style('university_main_styles', get_stylesheet_uri());
}

add_action('wp_enqueue_scripts', 'university_files');

function university_features() {
  add_theme_support('title-tag');
  add_theme_support('post-thumbnails');
  add_image_size('professorLandscape', 400, 260, true);
  add_image_size('professorPortrait', 480, 650, true);
  add_image_size('pageBanner', 1500, 350, true);
}

add_action('after_setup_theme', 'university_features');

function university_adjust_queries($query) {
  if (!is_admin() AND is_post_type_archive('program') AND is_main_query()) {
    $query->set('orderby', 'title');
    $query->set('order', 'ASC');
    $query->set('posts_per_page', -1);
  }

  if (!is_admin() AND is_post_type_archive('event') AND is_main_query()) {
    $today = date('Ymd');
    $query->set('meta_key', 'event_date');
    $query->set('orderby', 'meta_value_num');
    $query->set('order', 'ASC');
    $query->set('meta_query', array(
              array(
                'key' => 'event_date',
                'compare' => '>=',
                'value' => $today,
                'type' => 'numeric'
              )
            ));
  }
}

add_action('pre_get_posts', 'university_adjust_queries');

function universityMapKey($api) {
  $api['key'] = 'AIzaSyCebykgkcMOnqg4KgonLWuJ2IUgG0S2xG4';
  return $api;
}

add_filter('acf/fields/google_map/api', 'universityMapKey');
Enter fullscreen mode Exit fullscreen mode

Discussion (13)

Collapse
simonscholz profile image
SIM

Hi,

I'm not that familiar with WordPress but I try to help you.

The error comes Frome the get_field function which should return an array with the fields sizes and pageBanner, but instead it returns an string.

Could you find and post the get field function.

Simon

Collapse
simonscholz profile image
SIM

Nvm, I found a documentation for the get_field function.

Give me a few minutes I'll check that and get back to you.

Collapse
simonscholz profile image
SIM

Hi,

could you paste the following in the functions.php script at line 12:

var_dump(get_field('page_banner_background_image'));
var_dump(get_field('page_banner_bg-image'));

Then reload the site where the error occurred and before the two errors you posted, there should be the output of the two lines you pasted.

I need the output to see what the function is returning.

Thanks

Simon

Thread Thread
manuelalonge profile image
Manuel Alonge Author • Edited

Hi Simon, thanks for help. Below the output of the two lines. What are they needed for? Sorry but I also don't know much about Wordpress, I am learning in these days.

C:\wamp6456\www\fictional-university\wp-content\themes\fictional-university-theme\functions.php: 12 string '89' (length= 2)
C:\wamp6456\www\fictional-university\wp-content\themes\fictional-university-theme\functions.php: 13 null

Thread Thread
simonscholz profile image
SIM • Edited

Hi,

where did you attached the output of the two lines? I don't see anything here or in your post.

// Now it's there, thanks

The var_dump function displays information about a variable or parameter you give in the function:
php.net/manual/en/function.var-dum...

Thread Thread
manuelalonge profile image
Manuel Alonge Author

I have put it in the post later because I could not attach it.

Collapse
simonscholz profile image
SIM

One more question,

do you miss something from the site? Or do you only want to clear the errors?

If you want to clear the errors:

Replace line 15 with this (If you are running php7 ):

$args['photo'] = get_field('page_banner_background_image')['sizes']['pageBanner'] ?? get_theme_file_uri('/images/ocean.jpg');

You can also delete the two lines (var_dump) you recently pasted.

Simon

Collapse
manuelalonge profile image
Manuel Alonge Author

I think I do not miss anything, I just want to clear the erros out. I will try with your suggestion and let you know.

Collapse
simonscholz profile image
SIM • Edited

Hi

Sorry I missed something. Actually you should replace line 14 with this:

if (get_field('page_banner_background_image') && isset(get_field('page_banner_background_image')['sizes']) && isset(get_field('page_banner_background_image')['sizes']['pageBanner'])) {

This should work.

Simon

Thread Thread
manuelalonge profile image
Manuel Alonge Author

Solved it! Thank you very much for your help!

Thread Thread
simonscholz profile image
SIM

No problem.

It could be possible that if it's a script or a plugin of WordPress itself, that after an update your replace of line 14 is getting reverted. Maybe you have to do it again then. Just as an information for you.

Have a nice day :)

Simon

Thread Thread
manuelalonge profile image
Manuel Alonge Author • Edited

Work perfectly Thanks

Thread Thread
manuelalonge profile image
Manuel Alonge Author

Wish you a nice day as well :)