Jump to content

Sent custom variable to a hook function


jgshier

Recommended Posts

Total newbe here, I have created a hook to run a php function, that function goes and curls a site and gives back an array of category_id and category_name. Which I want to display on my template page. 

My template page, I am doing:

{foreach from=$categories item=items}
    List : {$items.category_id} - {$items.category_name}<br>
{/foreach}


Now I want to take the category_id and pass it back to a new function (hook_get_titles) which will give back titles that are in that category. How can I pass the category_id back ?  
function hook_get_categories($vars)
{

//Action

    $url = 'some webpage';

    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_SSL_VERIFYHOST => 0,
        CURLOPT_SSL_VERIFYPEER => 0,
        CURLINFO_HEADER_OUT => true,
        CURLOPT_HEADER => false,

    ));
    $content = curl_exec($ch);
    //$information = curl_getinfo($ch);
    curl_close($ch);

    $categories = json_decode($content, true);

    return array("categories" => $categories);


}
add_hook('ClientAreaPage', 1, 'hook_get_categories');

function hook_get_titles($categories_id)
{
    $url = 'some webpage/?categories_id='.$categories_id;

    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_SSL_VERIFYHOST => 0,
        CURLOPT_SSL_VERIFYPEER => 0,
        CURLINFO_HEADER_OUT => true,
        CURLOPT_HEADER => false
    ));

    $content = curl_exec($ch);
    curl_close($ch);

    $titles = json_decode($content, true);
    return array("titles" => $titles);
}

add_hook('ClientAreaPage', 1, 'hook_get_title');

 

Link to comment
Share on other sites

modify the first function to pull the titles within foreach loop like bellow:

function hook_get_categories($vars)
{

//Action

    $url = 'some webpage';

    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_SSL_VERIFYHOST => 0,
        CURLOPT_SSL_VERIFYPEER => 0,
        CURLINFO_HEADER_OUT => true,
        CURLOPT_HEADER => false,

    ));
    $content = curl_exec($ch);
    //$information = curl_getinfo($ch);
    curl_close($ch);
	

    $categories = json_decode($content, true);

	$categorylist = array();

	foreach ($categories as $category){
		$category['titles'] = hook_get_titles($category['category_id']);
		$categorylist[] = $category;
	}

    return array("categories" => $categorylist);


}

then modify your template foreach loop to the following:

{foreach from=$categories item=items}
    List : {$items.category_id} - {$items.category_name}<br>
	{foreach from=$items.titles key=index item=title}Title #{$index}: {$title}<br>{/foreach}
	
	-------<br>
{/foreach}

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated