I have this WordPress function:
function content_before_after($content) {
return 'something goes here';
}
add_filter('the_content', 'content_before_after');
...which works fine except for one small problem: If there is more than one instance of the_content() on any page template, the returned text will appear for each of them.
For reasons I can't go into I won't be able to modify the templates.
But the problem I need to be solved is: how can I change this function so that the returned text is only output into the first instance of the_content() of my page template?
Here static variable will help you:
function content_before_after($content) {
// define variable as static
static $content_shown;
// if variable has __no__ value
// it means we run function for the first time
if (!$content_shown) {
// change value and return required string
$content_shown = true;
return 'something goes here';
}
// empty string will be returned in second and other function calls
return '';
}
Related
I have navigation with product categories at top of website.
Want to repeat this navigation in footer of website.
Instead of writing $args, defining variables and such once again I decided to make a function for this.
It looks like this:
However as every function should have a return I'm confused how should return of this function look like?
When I try to return <li> instead of echoing it - I get nothing, empty area, but with echo - function works.
Instead of echo you can write $return_data .= ... to concatenate a string which was initially declared as an empty string before the loop began.
Here is a sample for such cases:
function show_prod_cats($all_categories) {
$return_data = '';
foreach ($all_categories as $cat) {
//echo '<li>....</li>';
$return_data .= '<li>....</li>';
}
return $return_data;
}
I've been racking my brains over this one but I'll do my best to describe the problem as best as possible. I have a custom function written within template.php, with a bunch of conditionals. When a condition is true, I would like to assign a value to a variable, and then pass that variable intro a node preprocess function that allows that variables to be rendered on a node template.
The function containing the condition:
function _mytheme_date_repeat_string($vars) {
$exdate_pos = strpos($rrule['WKST'], 'EXDATE:');
if($exdate_pos > 0) {
$vars['testvar'] = 'abc123';
}
}
The preprocess function that I would like to render the variable in for node template use:
function mytheme_preprocess_node(&$vars, $hook) {
$vars['new_variable'] = $testvar;
}
Intended usage in node.tpl.php:
<?php print $new_variable; ?>
I'm not great with PHP, but I know enough about programming to know that variable scope might be an issue here. What would be the best way to implement this? Any guidance is greatly appreciated.
Thanks,
Mark.
If it is not called, your _mytheme_date_repeat_string() function will never be executed. Preprocess functions (ie. any function starting mytheme_preprocess_, are called automatically by Drupal's theme system.
What you need is either move the code of _mytheme_date_repeat_string() in mytheme_preprocess_node() or refactor it and call it.
function _mytheme_date_repeat_string($rrule) {
$exdate_pos = strpos($rrule['WKST'], 'EXDATE:');
if($exdate_pos > 0) {
return 'abc123';
}
else {
return NULL;
}
}
/**
* Prepares variables for node templates.
*/
function mytheme_preprocess_node(&$variables, $hook) {
// Get $rrule from somewhere
$rrule = ... ;
$testvar = _mytheme_date_repeat_string($rrule);
if ($testvar) {
$variables['new_variable'] = $testvar;
}
}
You code does not show where the $rrule calue comes from. I assume you would get it for $variables['node'].
So I've built a small conditional to evaluate which button is pressed in my form (as there are 2). This works fine and fires off the correct method and writes the appropriate data to the DB, however my redirect is not working. It saves() to the DB and then simply stays on the page designated as the POST route.
I suspect the problem has something to do with my conditional and the use of $this.
Here is my check_submit method:
public function check_submit()
{
if(!is_null(Input::get('add_to_invoice'))){
$this->invoice_add_item();
} elseif(!is_null(Input::get('complete_invoice'))) {
$this->invoice_complete();
}
}
Here is one of the 2 methods which I am currently testing:
public function invoice_add_item()
{
$input = Request::all();
$invoice_items = new Expense;
$invoice_items->item_id = $input['item_id'];
$invoice_items->category_id = $input['category'];
$invoice_items->price = $input['price'];
$invoice_items->store_id = $input['store'];
if(Input::has('business_expense'))
{
$invoice_items->business_expense = 1;
}
else{
$invoice_items->business_expense = 0;
}
$invoice_items->save();
return redirect('/');
}
Perhaps there is a better way of handling this in my routes(web) file, but I'm not sure how to go about this.
You should add the return to the check_submit() method. Something like
public function check_submit()
{
if(!is_null(Input::get('add_to_invoice'))){
return $this->invoice_add_item();
} elseif(!is_null(Input::get('complete_invoice'))) {
return $this->invoice_complete();
}
}
Better yet, you should probably return a boolean on invoice_add_item() and based on that, redirect the user to the correct place (or with some session flash variable with an error message)
In my theme function.php i am trying to add shortcodes for myHeader, myFooter etc .
Inside myHeader(), myFooter() function i added another function like fn_1(), fn_2(), fn3_() etc these function would be change weekly or monthly basis.
Is it possible to call a shortcode as written below
function myHeader(){
fn_1();
//fn_2();
}
function myFooter(){
fn_2();
// fn_3();
}
add_shortcode('myFooter', 'myHeader');
add_shortcode('myFooter', 'myFooter');
function fn_1(){
return 'something for 1';
}
function fn_2(){
return 'something for 2';
}
function fn_3(){
return 'something for 3';
}
In my post i call my shortcode as [myHeader] and [myFooter]
It is possible, you just need to return something inside your shortcode methods. Shortcode functions also require some variables, though they don't actually have to be used.
eg.
function myHeader($atts, $content = null){
$temp = fn_1();
return $temp;
}
$atts is required to create shortcode if you are passing $atts or not.
I'm successfully updating the innerHTML of a DIV through xajax (when I click a link) but only when I assign HTML in the function itself, not when I call it from a different function.
To explain
// For the sake of testing
$output=rand(20,40);
// Add new HTML to container through $output
$ajax_resp->assign('div_container','innerHTML', $output);
return $ajax_resp;
This works fine. When I call this function through clicking the link, the container updates with a random number. However when I change $output to
$output=$compile->show('text');
Which is (simplified)
function show($var) { echo $var; }
It does not show it. The function show works perfectly outside of xajax. Any suggestions?
function show($var) { echo $var; }
This function doesn't return anything, it echo's the value to the screen. However, most functions you probably want to return a value, so they can work with it. Basically, when you do
$output=$compile->show('text');
It will get no input from the show method, because show doesn't return any value. I think if you change it to the following:
function show($var) { return $var; }
It will work.
Have you tried to change function show($var) { echo $var; } to function show($var) { return $var; }. Should solve your problem