How to call wordpress basic functions from functions.php file call

Hi Guys ,

I am simply calling this file –

/functions.php?fcall=update_cats_name'

And in functions.php file , I wrote this code –

$fcall = $_REQUEST['fcall'];
if($fcall && $fcall == 'update_cats_name'){
update_custom_category_name();
}
add_action("wp_ajax_update_custom_category_name", "update_custom_category_name");
function update_custom_category_name(){
$update_result = wp_update_term(25, 'goals-category', array(
'name' => $_POST['value']
));
echo 'done';
exit;
}

But it is throwing this error-

Fatal error: Call to undefined function wp_update_term() in D:wampwwwprojectsSGTwordpresswp-contentthemessgtfunctions.php on line 41

Please help me to solve this .

Thanks

  • Jude
    • DEV MAN

    Hi there @Raitis

    Can you check if $_POST has a value when this function is called. Looks like the function is called with an invalid method signature.

    Try something like

    if ( !is_null($_POST['value'] ) )
    {
    $update_result = wp_update_term(25, 'goals-category', array(
    'name' => $_POST['value']
    ));
    }

  • Raitis
    • Site Builder, Child of Zeus

    Hi Jude ,

    Thanks for your reply .

    Lets pass ‘value’ as static , still the same error – ‘Fatal error: Call to undefined function’. Also I found that I am not able to write any other core wordpress function also , like – ‘bloginfo’ or other ….

    Is there any other way to use/load core function

    Thanks

  • Raitis
    • Site Builder, Child of Zeus

    Hi @Jude ,

    Thanks for your suggestion here .

    require('./wp-load.php');

    this was throwing errors in api calls that some functions are already defined ,

    So I used this method –

    I created a file with name – ‘apicalls.php’ in themes/mytheme folder , then used this file for for api calls (like – apicalls.php?fcall=update_cats_name) instead of functions.php file ,

    for all wordpress functions to keep working I additionally added this line in top of file –

    require_once("../../../wp-config.php");

    and then put a check for function to render –

    $fcall = $_REQUEST;

    if($fcall && $fcall == ‘update_cats_name’:wink:{

    require_once(‘../../../wp-load.php’:wink:;

    function update_custom_category_name(){

    }

  • Jude
    • DEV MAN

    Hey @Raitis

    Glad you got it working !!

    I would still not recommend a require statement inside a conditional. Its not good practice. If it serves your purposes then great. Also thanks for posting back a solution for the benefit of others.

    Cheers

    Jude