Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

PHP Syntax error WordPress

  • SOLVED

$post_types = implode(',', array_map(function($post_type) { return "'" . esc_sql($post_type) . "'"; }, array_merge($builtin_post_types, $post_types)) );

leads to Parse error: syntax error, unexpected T_FUNCTION, expecting ')' in ...

cant find out what is causing this - can anyone help please?
thx

Answers (4)

2016-11-04

Rempty answers:

What version of PHP are using?
Only PHP 5.3 or later versions enables use of inline anonymous functions with array_map


Robert Seyfriedsberger comments:

this could be, as code works properly on my server - asked the user for verification if php 5.2 is used.

do you happen to know how to change this code to make it run on phone 5.2 too?

2016-11-04

Kyle answers:

Are you trying to return the quotations here:

return "'" . esc_sql($post_type) . "'";

2016-11-04

Luis Abarca answers:

$post_types = implode(',', array_map(function($post_type)
{
return sprintf( "'%s'", esc_sql($post_type) );
},
array_merge($builtin_post_types, $post_types)
) // End array_map
); // End implode

2016-11-04

Arnav Joy answers:

Can you let me know what you are trying to achieve ?


Robert Seyfriedsberger comments:

This is part of the following function (not written by me) which is used to detect where the shortcode to our plugin is used (posts, pages, CPT, widgets):
function lmm_get_map_shortcodes($id, $type){
global $wpdb;
$lmm_options = get_option( 'leafletmapsmarker_options' );
$shortcode = '[' . $lmm_options['shortcode'] . ' ' . $type . '="' . $id . '"';
$builtin_post_types = array('post', 'page');
$post_types = get_post_types(array('public' => true, '_builtin' => false));
$post_types = implode(',', array_map(function($post_type){ return "'" . esc_sql($post_type) . "'"; }, array_merge($builtin_post_types, $post_types)) );
$posts = $wpdb->get_results( $wpdb->prepare("SELECT ID,post_title FROM $wpdb->posts WHERE (post_type IN ($post_types) ) AND post_content LIKE %s ",'%'.$shortcode.'%') );
$result = '<ul style="margin:0;">';
foreach ($posts as $post) {
if ($post->post_title != NULL) { $post_title = $post->post_title; } else { $post_title = 'ID ' . $post->ID; }
$result .= '<li style="margin-bottom:0;clear:both;">' . ucfirst(get_post_type($post->ID)) . ': <a href="' . get_permalink($post->ID) . '" title="' . esc_attr__('view content','lmm') . '" target="_blank">' . $post_title . '</a>';
if(current_user_can('edit_others_posts')){
$result .= '<a style="float:right;" href="' . get_edit_post_link( $post->ID ) . '"> ('. __('edit','lmm') . ')</a>';
}
$result .= '</li>';
}
$widgets = get_option('widget_text');
if (!empty($widgets)):
foreach ($widgets as $w_key => $widget) {
$shortcode = '['.$lmm_options['shortcode'].' '.$type.'="'.$id.'"]';
if (is_array($widget)) {
if(isset($widget['text']) && $widget['text']!= ''){
if(strpos($shortcode, $widget['text']) !== FALSE) {
$result .= '<li style="margin-bottom:0;">';
$result .= sprintf(__('Found in a <a href="%1$s">widget</a>'), admin_url('widgets.php')) . '</a>';
$result .= '</li>';
}
}
}
}
endif;
$result .= '</ul>';
if ($result == '<ul style="margin:0;"></ul>') {
$result = __('not used in any content','lmm');
}
return $result;
}