Moving function from functions.php to plugin












0















I'm probably doing something stupid.



But i have the following working code, (this a small (but working) part of the code) if i have this code in my functions.php it works fine, but when i add it in a custom plugin, it does not



function update_booklink_field( $post_id ) {
if( ! ( wp_is_post_revision( $post_id) || wp_is_post_autosave( $post_id ) ) ) {

update_post_meta( $post_id, 'prijstest', 'testvalue' );
}
}
add_action( 'save_post', 'update_booklink_field' );


This is my code in the plugin, is there something obvious i am missing? Or are there extra steps i need to take to trigger a function when it is in a plugin?



<?php
/**
* Plugin Name: aaautofill
*/

function update_booklink_field( $post_id ) {
if( ! ( wp_is_post_revision( $post_id) || wp_is_post_autosave( $post_id ) ) ) {

update_post_meta( $post_id, 'prijstest', 'testvalue' );
}
}
add_action( 'save_post', 'update_booklink_field' );






?>


EDIT:



What i found out so far:
if i move




add_action( 'save_post', 'update_booklink_field');




to the functions file, then it works.
The plugin is activated, but the save_post just doesnt run when its in my plugin file. i really have 0 clue what is causing this :/



edit 2:



Basicly the function doesnt seem to have an issue, because if i change the location of when its loaded (in the fucntion.php) it works fine.




  1. Issue is the add_action save hook (probably) because

  2. plugin is live/active/works (tested with var dumbs)

  3. the function also works, if the add_action is in the functions.php vs when it's in the plugin file.


    1. no other plugins are live, default twenty seventeen, theme




Does this mean it has to do with the order in which the things are loaded? (or am i overlooking something stupid?)



Edit 3, i found it/got it working
if i changed the save_post line to:




add_action( 'save_post', 'update_booklink_field', 50 , 50 );




it works










share|improve this question

























  • What error are you getting? How is it "not working"?

    – Sara Tibbetts
    Nov 13 '18 at 16:03











  • i am not getting an error, it just doesnt update the field (it updates fine if that code is in the functions.php file though)

    – Phpnewb
    Nov 13 '18 at 16:07











  • Are you sure you activated the plugin? What happens if you just do a test var_dump like var_dump('testing'); outside the function? Does it show up on the front end?

    – git-e-up
    Nov 13 '18 at 16:09













  • yeh, the plugin is activated, and if i add "var_dump('testing');" outside that fuction, i see "string(7) "testing" on the front end

    – Phpnewb
    Nov 13 '18 at 16:16











  • That's good. That said, I don't know what the problem is, but maybe the $post_id variable isn't working. Maybe try defining it $post_id = get_the_ID();

    – git-e-up
    Nov 13 '18 at 16:26


















0















I'm probably doing something stupid.



But i have the following working code, (this a small (but working) part of the code) if i have this code in my functions.php it works fine, but when i add it in a custom plugin, it does not



function update_booklink_field( $post_id ) {
if( ! ( wp_is_post_revision( $post_id) || wp_is_post_autosave( $post_id ) ) ) {

update_post_meta( $post_id, 'prijstest', 'testvalue' );
}
}
add_action( 'save_post', 'update_booklink_field' );


This is my code in the plugin, is there something obvious i am missing? Or are there extra steps i need to take to trigger a function when it is in a plugin?



<?php
/**
* Plugin Name: aaautofill
*/

function update_booklink_field( $post_id ) {
if( ! ( wp_is_post_revision( $post_id) || wp_is_post_autosave( $post_id ) ) ) {

update_post_meta( $post_id, 'prijstest', 'testvalue' );
}
}
add_action( 'save_post', 'update_booklink_field' );






?>


EDIT:



What i found out so far:
if i move




add_action( 'save_post', 'update_booklink_field');




to the functions file, then it works.
The plugin is activated, but the save_post just doesnt run when its in my plugin file. i really have 0 clue what is causing this :/



edit 2:



Basicly the function doesnt seem to have an issue, because if i change the location of when its loaded (in the fucntion.php) it works fine.




  1. Issue is the add_action save hook (probably) because

  2. plugin is live/active/works (tested with var dumbs)

  3. the function also works, if the add_action is in the functions.php vs when it's in the plugin file.


    1. no other plugins are live, default twenty seventeen, theme




Does this mean it has to do with the order in which the things are loaded? (or am i overlooking something stupid?)



Edit 3, i found it/got it working
if i changed the save_post line to:




add_action( 'save_post', 'update_booklink_field', 50 , 50 );




it works










share|improve this question

























  • What error are you getting? How is it "not working"?

    – Sara Tibbetts
    Nov 13 '18 at 16:03











  • i am not getting an error, it just doesnt update the field (it updates fine if that code is in the functions.php file though)

    – Phpnewb
    Nov 13 '18 at 16:07











  • Are you sure you activated the plugin? What happens if you just do a test var_dump like var_dump('testing'); outside the function? Does it show up on the front end?

    – git-e-up
    Nov 13 '18 at 16:09













  • yeh, the plugin is activated, and if i add "var_dump('testing');" outside that fuction, i see "string(7) "testing" on the front end

    – Phpnewb
    Nov 13 '18 at 16:16











  • That's good. That said, I don't know what the problem is, but maybe the $post_id variable isn't working. Maybe try defining it $post_id = get_the_ID();

    – git-e-up
    Nov 13 '18 at 16:26
















0












0








0








I'm probably doing something stupid.



But i have the following working code, (this a small (but working) part of the code) if i have this code in my functions.php it works fine, but when i add it in a custom plugin, it does not



function update_booklink_field( $post_id ) {
if( ! ( wp_is_post_revision( $post_id) || wp_is_post_autosave( $post_id ) ) ) {

update_post_meta( $post_id, 'prijstest', 'testvalue' );
}
}
add_action( 'save_post', 'update_booklink_field' );


This is my code in the plugin, is there something obvious i am missing? Or are there extra steps i need to take to trigger a function when it is in a plugin?



<?php
/**
* Plugin Name: aaautofill
*/

function update_booklink_field( $post_id ) {
if( ! ( wp_is_post_revision( $post_id) || wp_is_post_autosave( $post_id ) ) ) {

update_post_meta( $post_id, 'prijstest', 'testvalue' );
}
}
add_action( 'save_post', 'update_booklink_field' );






?>


EDIT:



What i found out so far:
if i move




add_action( 'save_post', 'update_booklink_field');




to the functions file, then it works.
The plugin is activated, but the save_post just doesnt run when its in my plugin file. i really have 0 clue what is causing this :/



edit 2:



Basicly the function doesnt seem to have an issue, because if i change the location of when its loaded (in the fucntion.php) it works fine.




  1. Issue is the add_action save hook (probably) because

  2. plugin is live/active/works (tested with var dumbs)

  3. the function also works, if the add_action is in the functions.php vs when it's in the plugin file.


    1. no other plugins are live, default twenty seventeen, theme




Does this mean it has to do with the order in which the things are loaded? (or am i overlooking something stupid?)



Edit 3, i found it/got it working
if i changed the save_post line to:




add_action( 'save_post', 'update_booklink_field', 50 , 50 );




it works










share|improve this question
















I'm probably doing something stupid.



But i have the following working code, (this a small (but working) part of the code) if i have this code in my functions.php it works fine, but when i add it in a custom plugin, it does not



function update_booklink_field( $post_id ) {
if( ! ( wp_is_post_revision( $post_id) || wp_is_post_autosave( $post_id ) ) ) {

update_post_meta( $post_id, 'prijstest', 'testvalue' );
}
}
add_action( 'save_post', 'update_booklink_field' );


This is my code in the plugin, is there something obvious i am missing? Or are there extra steps i need to take to trigger a function when it is in a plugin?



<?php
/**
* Plugin Name: aaautofill
*/

function update_booklink_field( $post_id ) {
if( ! ( wp_is_post_revision( $post_id) || wp_is_post_autosave( $post_id ) ) ) {

update_post_meta( $post_id, 'prijstest', 'testvalue' );
}
}
add_action( 'save_post', 'update_booklink_field' );






?>


EDIT:



What i found out so far:
if i move




add_action( 'save_post', 'update_booklink_field');




to the functions file, then it works.
The plugin is activated, but the save_post just doesnt run when its in my plugin file. i really have 0 clue what is causing this :/



edit 2:



Basicly the function doesnt seem to have an issue, because if i change the location of when its loaded (in the fucntion.php) it works fine.




  1. Issue is the add_action save hook (probably) because

  2. plugin is live/active/works (tested with var dumbs)

  3. the function also works, if the add_action is in the functions.php vs when it's in the plugin file.


    1. no other plugins are live, default twenty seventeen, theme




Does this mean it has to do with the order in which the things are loaded? (or am i overlooking something stupid?)



Edit 3, i found it/got it working
if i changed the save_post line to:




add_action( 'save_post', 'update_booklink_field', 50 , 50 );




it works







php wordpress plugins






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 17:55







Phpnewb

















asked Nov 13 '18 at 16:01









PhpnewbPhpnewb

63




63













  • What error are you getting? How is it "not working"?

    – Sara Tibbetts
    Nov 13 '18 at 16:03











  • i am not getting an error, it just doesnt update the field (it updates fine if that code is in the functions.php file though)

    – Phpnewb
    Nov 13 '18 at 16:07











  • Are you sure you activated the plugin? What happens if you just do a test var_dump like var_dump('testing'); outside the function? Does it show up on the front end?

    – git-e-up
    Nov 13 '18 at 16:09













  • yeh, the plugin is activated, and if i add "var_dump('testing');" outside that fuction, i see "string(7) "testing" on the front end

    – Phpnewb
    Nov 13 '18 at 16:16











  • That's good. That said, I don't know what the problem is, but maybe the $post_id variable isn't working. Maybe try defining it $post_id = get_the_ID();

    – git-e-up
    Nov 13 '18 at 16:26





















  • What error are you getting? How is it "not working"?

    – Sara Tibbetts
    Nov 13 '18 at 16:03











  • i am not getting an error, it just doesnt update the field (it updates fine if that code is in the functions.php file though)

    – Phpnewb
    Nov 13 '18 at 16:07











  • Are you sure you activated the plugin? What happens if you just do a test var_dump like var_dump('testing'); outside the function? Does it show up on the front end?

    – git-e-up
    Nov 13 '18 at 16:09













  • yeh, the plugin is activated, and if i add "var_dump('testing');" outside that fuction, i see "string(7) "testing" on the front end

    – Phpnewb
    Nov 13 '18 at 16:16











  • That's good. That said, I don't know what the problem is, but maybe the $post_id variable isn't working. Maybe try defining it $post_id = get_the_ID();

    – git-e-up
    Nov 13 '18 at 16:26



















What error are you getting? How is it "not working"?

– Sara Tibbetts
Nov 13 '18 at 16:03





What error are you getting? How is it "not working"?

– Sara Tibbetts
Nov 13 '18 at 16:03













i am not getting an error, it just doesnt update the field (it updates fine if that code is in the functions.php file though)

– Phpnewb
Nov 13 '18 at 16:07





i am not getting an error, it just doesnt update the field (it updates fine if that code is in the functions.php file though)

– Phpnewb
Nov 13 '18 at 16:07













Are you sure you activated the plugin? What happens if you just do a test var_dump like var_dump('testing'); outside the function? Does it show up on the front end?

– git-e-up
Nov 13 '18 at 16:09







Are you sure you activated the plugin? What happens if you just do a test var_dump like var_dump('testing'); outside the function? Does it show up on the front end?

– git-e-up
Nov 13 '18 at 16:09















yeh, the plugin is activated, and if i add "var_dump('testing');" outside that fuction, i see "string(7) "testing" on the front end

– Phpnewb
Nov 13 '18 at 16:16





yeh, the plugin is activated, and if i add "var_dump('testing');" outside that fuction, i see "string(7) "testing" on the front end

– Phpnewb
Nov 13 '18 at 16:16













That's good. That said, I don't know what the problem is, but maybe the $post_id variable isn't working. Maybe try defining it $post_id = get_the_ID();

– git-e-up
Nov 13 '18 at 16:26







That's good. That said, I don't know what the problem is, but maybe the $post_id variable isn't working. Maybe try defining it $post_id = get_the_ID();

– git-e-up
Nov 13 '18 at 16:26














1 Answer
1






active

oldest

votes


















0














Perhaps this will do the trick:



<?php
/**
* Plugin Name: aaautofill
*/

function update_booklink_field( ) {

global $post;

if( ! ( wp_is_post_revision( $post->ID ) || wp_is_post_autosave( $post->ID ) ) ) {

update_post_meta( $post->ID, 'prijstest', 'testvalue' );
}
}
add_action( 'save_post', 'update_booklink_field' );

?>





share|improve this answer
























  • thank you for having a look, but afraid not, same issue again, if i have that in my plugin, it doesnt work. if i remove the add_action line, and add that part in my functions.php. it works fine again

    – Phpnewb
    Nov 13 '18 at 17:32











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284900%2fmoving-function-from-functions-php-to-plugin%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Perhaps this will do the trick:



<?php
/**
* Plugin Name: aaautofill
*/

function update_booklink_field( ) {

global $post;

if( ! ( wp_is_post_revision( $post->ID ) || wp_is_post_autosave( $post->ID ) ) ) {

update_post_meta( $post->ID, 'prijstest', 'testvalue' );
}
}
add_action( 'save_post', 'update_booklink_field' );

?>





share|improve this answer
























  • thank you for having a look, but afraid not, same issue again, if i have that in my plugin, it doesnt work. if i remove the add_action line, and add that part in my functions.php. it works fine again

    – Phpnewb
    Nov 13 '18 at 17:32
















0














Perhaps this will do the trick:



<?php
/**
* Plugin Name: aaautofill
*/

function update_booklink_field( ) {

global $post;

if( ! ( wp_is_post_revision( $post->ID ) || wp_is_post_autosave( $post->ID ) ) ) {

update_post_meta( $post->ID, 'prijstest', 'testvalue' );
}
}
add_action( 'save_post', 'update_booklink_field' );

?>





share|improve this answer
























  • thank you for having a look, but afraid not, same issue again, if i have that in my plugin, it doesnt work. if i remove the add_action line, and add that part in my functions.php. it works fine again

    – Phpnewb
    Nov 13 '18 at 17:32














0












0








0







Perhaps this will do the trick:



<?php
/**
* Plugin Name: aaautofill
*/

function update_booklink_field( ) {

global $post;

if( ! ( wp_is_post_revision( $post->ID ) || wp_is_post_autosave( $post->ID ) ) ) {

update_post_meta( $post->ID, 'prijstest', 'testvalue' );
}
}
add_action( 'save_post', 'update_booklink_field' );

?>





share|improve this answer













Perhaps this will do the trick:



<?php
/**
* Plugin Name: aaautofill
*/

function update_booklink_field( ) {

global $post;

if( ! ( wp_is_post_revision( $post->ID ) || wp_is_post_autosave( $post->ID ) ) ) {

update_post_meta( $post->ID, 'prijstest', 'testvalue' );
}
}
add_action( 'save_post', 'update_booklink_field' );

?>






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 17:23









VosVos

964




964













  • thank you for having a look, but afraid not, same issue again, if i have that in my plugin, it doesnt work. if i remove the add_action line, and add that part in my functions.php. it works fine again

    – Phpnewb
    Nov 13 '18 at 17:32



















  • thank you for having a look, but afraid not, same issue again, if i have that in my plugin, it doesnt work. if i remove the add_action line, and add that part in my functions.php. it works fine again

    – Phpnewb
    Nov 13 '18 at 17:32

















thank you for having a look, but afraid not, same issue again, if i have that in my plugin, it doesnt work. if i remove the add_action line, and add that part in my functions.php. it works fine again

– Phpnewb
Nov 13 '18 at 17:32





thank you for having a look, but afraid not, same issue again, if i have that in my plugin, it doesnt work. if i remove the add_action line, and add that part in my functions.php. it works fine again

– Phpnewb
Nov 13 '18 at 17:32


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284900%2fmoving-function-from-functions-php-to-plugin%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Coverage of Google Street View

Full-time equivalent

Surfing