Cannot redeclare two functions - php

How can I solve the following issue with the code attached? Seems that somehow Wordpress (or some kind of plugin) is calling the function twice.
function my_wpcf7_form_elements($html) {
function ov3rfly_replace_include_blank($name, $text, &$html) {
$matches = false;
preg_match('/<select name="' . $name . '"[^>]*>(.*)<\/select>/iU', $html, $matches);
if ($matches) {
$select = str_replace('<option value="">---</option>', '<option value="">' . $text . '</option>', $matches[0]);
$html = preg_replace('/<select name="' . $name . '"[^>]*>(.*)<\/select>/iU', $select, $html);
}
}
ov3rfly_replace_include_blank('countrylist', 'España', $html);
return $html;
}
add_filter('wpcf7_form_elements', 'my_wpcf7_form_elements');
Fatal error: Cannot redeclare ov3rfly_replace_include_blank() (previously declared in /Applications/XAMPP/xamppfiles/htdocs/w/wp-content/themes/bulwark_child/functions.php:21) in /Applications/XAMPP/xamppfiles/htdocs/w/wp-content/themes/bulwark_child/functions.php on line 21

Dont nest the functions - your current code declares the inner function every time the outer function is called, thereby causing the error the second time:
function ov3rfly_replace_include_blank($name, $text, &$html) {
$matches = false;
preg_match('/<select name="' . $name . '"[^>]*>(.*)<\/select>/iU', $html, $matches);
if ($matches) {
$select = str_replace('<option value="">---</option>', '<option value="">' . $text . '</option>', $matches[0]);
$html = preg_replace('/<select name="' . $name . '"[^>]*>(.*)<\/select>/iU', $select, $html);
}
}
function my_wpcf7_form_elements($html) {
ov3rfly_replace_include_blank('countrylist', 'España', $html);
return $html;
}
add_filter('wpcf7_form_elements', 'my_wpcf7_form_elements');

Check this file for re declare function as error message suggested
/Applications/XAMPP/xamppfiles/htdocs/w/wp-content/themes/bulwark_child/functions.php:21
rename one function and see whether it working or not
Write a separate function,multiple function calling in nested function:
function my_wpcf7_form_elements($html) {
ov3rfly_replace_include_blank('countrylist', 'España', $html);
return $html;
}
function ov3rfly_replace_include_blank($name, $text, &$html) {
$matches = false;
preg_match('/<select name="' . $name . '"[^>]*>(.*)<\/select>/iU', $html, $matches);
if ($matches) {
$select = str_replace('<option value="">---</option>', '<option value="">' . $text . '</option>', $matches[0]);
$html = preg_replace('/<select name="' . $name . '"[^>]*>(.*)<\/select>/iU', $select, $html);
}
}
add_filter('wpcf7_form_elements', 'my_wpcf7_form_elements');

Related

can't figure out ou the return preg_replace with 2 array

I can't figure out the return of this function...Thank's a lot!
function convert_links($string, $base) {
$pattern = array(',<(a|link|img|script|form|td)([^>]+)(href|src|action|background)="([^>"\s]+)",ie',
',<(a|link|img|script|form|td)([^>]+)(href|src|action|background)=([^>"\s]+),ie',
',<style(.+)url\("([^>"\s]+)",ie');
$replace = array('"<\1\2\3=\"" . absolute_link("\4", $base,"1 \1") . "\""',
'"<\1\2\3=\"" . absolute_link("\4", $base,"2 \1") . "\""',
'"<style\lurl(\"" . absolute_link("\2", $base) . "\""');
return preg_replace($pattern, $replace, $string);

how to limit numwords article in codeigniter?

i have cms blog with codeigniter. but when i limit content in article is not working. i am newbie in codeigntier can you tell me what am i to do.
this is cms_helper.php
function get_excerpt($article, $numwords = 20){
$string = '';
$url = article_link($article);
$string .= '<h2>' . anchor($url, e($article->judul_berita)) . '</h2>';
$string .= '<p class="pubdate">' . e($article->tanggal) . '</p>';
$string .= '<p>' . e(limit_to_numwords(strip_tags($article->content), $numwords)) . '</p>';// content is not show
$string .= '<p>' . anchor($url, 'Read more', array('judul_berita' => e($article->judul_berita))) . '</p>';
return $string;
}
function limit_to_numwords($string, $numwords){
$excerpt = explode(' ', $string, $numwords + 1);
if (count($excerpt) >= $numwords) {
array_pop($excerpt);
}
$excerpt = implode(' ', $excerpt);
return $excerpt;
}
function e($string){
return htmlentities($string);
}
in controller
private function _homepage()
{
$this->load->model('mberita');
$this->db->limit(6);
$this->data['articles'] = $this->mberita->get_berita();
//var_dump($this->data['articles']);
}
please help me what to do. thank you.
You should use Codeigniter text helper:
$this->load->helper('text'); // add this to the the function or to the constructor
$string .= '<p>' . e(word_limiter(strip_tags($article->content), $numwords)) . '</p>';
In this article you can find many useful codeigniter text functions
Try the below sample. this will surely work.
function limit_to_numwords($text,$no_words){
$next=substr($text,$no_words,strlen($text));
$spacepos=strpos($next," ");
$desc=substr($text,0,$no_words+$spacepos)."...";
return $desc;
}
From this, you can get exact full word text from the nearest space character. hope this will help your requirement.
Also, try to call the function as "$this->limit_to_numwords"

How do I access a variable inside of preg_replace_callback?

I'm trying to replace {{key}} items in my $text with values from a passed array. but when I tried adding the print_r to see what was going on I got a Undefined variable: kvPairs error. How can I access my variable form within the preg_replace_callback?
public function replaceValues($kvPairs, $text) {
$text = preg_replace_callback(
'/(\{{)(.*?)(\}})/',
function ($match) {
$attr = trim($match[2]);
print_r($kvPairs[strtolower($attr)]);
if (isset($kvPairs[strtolower($attr)])) {
return "<span class='attr'>" . $kvPairs[strtolower($attr)] . "</span>";
} else {
return "<span class='attrUnknown'>" . $attr . "</span>";
}
},
$text
);
return $text;
}
Update:
I've tried the global scope thing, but it doesn't work either. I've added 2 print statements to see whats doing on, one inside and one outside the preg_replace_callback.
public function replaceValues($kvPairs, $text) {
$attrTest = 'date';
print_r("--" . strtolower($attrTest) . "--" . $kvPairs[strtolower($attrTest)] . "--\n");
$text = preg_replace_callback(
'/(\{{)(.*?)(\}})/',
function ($match) {
global $kvPairs;
$attr = trim($match[2]);
print_r("==" . strtolower($attr) . "==" . $kvPairs[strtolower($attr)] . "==\n");
if (isset($kvPairs[strtolower($attr)])) {
return "<span class='attr'>" . $kvPairs[strtolower($attr)] . "</span>";
} else {
return "<span class='attrUnknown'>" . $attr . "</span>";
}
},
$text
);
return $text;
}
The output I get is:
--date--1977-05-20--
==date====
As your callback function is a closure, you can pass extra arguments via use
function ($match) use ($kvPairs) {
...
}
better than polluting the global space

"fatal error: Cannot redeclare" while changing "---" title on <select> element in contact form 7 function

this is the url to the test site - http://gil.beastserv.com/hava/
i have this function, with some help from here
function my_wpcf7_form_elements($html) {
function ov3rfly_replace_include_blank($name, $text, &$html) {
$matches = false;
preg_match('/<select name="' . $name . '"[^>]*>(.*)<\/select>/iU', $html, $matches);
if ($matches) {
$select = str_replace('<option value="">---</option>', '<option value="">' . $text . '</option>', $matches[0]);
$html = preg_replace('/<select name="' . $name . '"[^>]*>(.*)<\/select>/iU', $select, $html);
}
}
ov3rfly_replace_include_blank('c_age', 'גיל', $html);
ov3rfly_replace_include_blank('c_area', 'איזור', $html);
ov3rfly_replace_include_blank('c_type', 'סוג ביטוח', $html);
ov3rfly_replace_include_blank('c_area', 'איזור', $html);
ov3rfly_replace_include_blank('c_cartype', 'סוג רכב', $html);
ov3rfly_replace_include_blank('c_manifacture', 'יצרן', $html);
ov3rfly_replace_include_blank('c_manifactureyear', 'שנת יצור', $html);
ov3rfly_replace_include_blank('c_driversage', 'גיל הנהג', $html);
ov3rfly_replace_include_blank('c_prevent', 'שלילות', $html);
ov3rfly_replace_include_blank('c_claim', 'תביעות', $html);
return $html;
}
add_filter('wpcf7_form_elements', 'my_wpcf7_form_elements');
its working great when there is only 1 form, but when im tring to place 2 forms in 1 page, im getting the Fatal error: Cannot redeclare ov3rfly_replace_include_blank() (previously declared
i tried to place the if (!function_exists('formatStr')) {}
as so:
if (!function_exists('formatStr')) {
function my_wpcf7_form_elements($html) {}
add_filter('wpcf7_form_elements', 'my_wpcf7_form_elements');
}
but i guess that its not the problem.. the function is called twice, because there are 2 forms.. how can i overcome this?
Thanks alot.
From http://es2.php.net/manual/en/functions.user-defined.php:
All functions and classes in PHP have the global scope - they can be
called outside a function even if they were defined inside and vice
versa.
PHP does not support function overloading, nor is it possible to
undefine or redefine previously-declared functions.
function ov3rfly_replace_include_blank is being declared every time you call my_wpcf7_form_elements, and PHP does not support function overloading, hence the error. Since all PHP functions have global scope, they can be called inside a function even if they where defined outside. Try:
function ov3rfly_replace_include_blank($name, $text, &$html) {
$matches = false;
preg_match('/<select name="' . $name . '"[^>]*>(.*)<\/select>/iU', $html, $matches);
if ($matches) {
$select = str_replace('<option value="">---</option>', '<option value="">' . $text . '</option>', $matches[0]);
$html = preg_replace('/<select name="' . $name . '"[^>]*>(.*)<\/select>/iU', $select, $html);
}
}
function my_wpcf7_form_elements($html) {
ov3rfly_replace_include_blank('c_age', 'גיל', $html);
ov3rfly_replace_include_blank('c_area', 'איזור', $html);
ov3rfly_replace_include_blank('c_type', 'סוג ביטוח', $html);
ov3rfly_replace_include_blank('c_area', 'איזור', $html);
ov3rfly_replace_include_blank('c_cartype', 'סוג רכב', $html);
ov3rfly_replace_include_blank('c_manifacture', 'יצרן', $html);
ov3rfly_replace_include_blank('c_manifactureyear', 'שנת יצור', $html);
ov3rfly_replace_include_blank('c_driversage', 'גיל הנהג', $html);
ov3rfly_replace_include_blank('c_prevent', 'שלילות', $html);
ov3rfly_replace_include_blank('c_claim', 'תביעות', $html);
return $html;
}
add_filter('wpcf7_form_elements', 'my_wpcf7_form_elements');

Problem returning excerpt with specified number of characters

I'm using the following code to grab a string and return a spesific number of characters
<div class="top_posts">
<ul>';
}
elseif($i>1 && $i<=$number_of_posts)
{
$retour.= "\n" . '<li>';
if($image_url) { $retour .= '<img src="' . get_bloginfo('template_directory') . '/js/timthumb.php?src=' . $image_url . '&h=120&w=180&zc=1" alt="" />'; }
$retour .= '<h6>' . the_title("","",false) . '</h6>';
$retour.= get_wpe_excerpt('wpe_popular_posts');
$retour.='</li>';
if($i%2==1)
$retour.= "\n" . '<li class="clear">&</li>';
}
$i++;
endforeach;
$retour.='</ul></div>';
return $retour;
}
add_shortcode('popular-posts', 'popular_posts_code');
The chunk at issue is this part
$retour.= get_wpe_excerpt('wpe_popular_posts');
which calls to
function wpe_popular_posts($length) {
return 55;
}
However I'm still getting the full text string untrimmed - any help appreciated.
//update
The get_wpe_excerpt function looks like this
function get_wpe_excerpt($length_callback='', $more_callback='') {
if(function_exists($length_callback)){
add_filter('excerpt_length', $length_callback);
}
if(function_exists($more_callback)){
add_filter('excerpt_more', $more_callback);
}
$output = get_the_excerpt();
$output = apply_filters('wptexturize', $output);
$output = apply_filters('convert_chars', $output);
$output = '<p>'.$output.'</p>';
return $output;
}
Now sure how the get_wpe_excerpt() function is done. You may want to try the simple code I used in several WordPress themes.
implode(' ', array_slice(explode(' ', get_the_content()), 0, 55));
You want to return the first 55 characters of the string?
Try substr($input, 0, 55);
However I really don't understand what's going on with that call to $retour.= get_wpe_excerpt('wpe_popular_posts');... so maybe I am misunderstanding what you are trying to achieve?

Categories