• hi @ all,

    i’ve got a problem by adding customfields into ma wiordpress theme. the name/id of the customfield shell be predefined, the Text/value shell be customizable.

    [php]

    add_action( ‘save_post’, ‘kb_custom_field_default’, 10, 2 );

    function kb_custom_field_default( $post_id, WP_Post $post ) {
    if (!get_post_meta( $post_id, ‘CUSTOM-FIELD-KEY’, true ) ) {
    update_post_meta( $post_id, ‘a, ‘value1’ );
    update_post_meta( $post_id, ‘b’, ‘value2’ );
    update_post_meta( $post_id, ‘c’, ‘value3’ );
    }
    }

    [/php]

    So how can i release this. ID/name is working but the value is always the one i typed into the functions.php (value1, value2, value3) how can i change that to custom text?

    best regards

Viewing 6 replies - 1 through 6 (of 6 total)
  • The best place to start is with adding a metabox. You can see the developer documentation here: https://developer.wordpress.org/reference/functions/add_meta_box/

    Once you’re comfortable with that, or if you need something more simplistic, check this out: https://wordpress.org/plugins/cmb2/

    Basically you need a metabox to actually accept the data, or your ‘customized values’. Once you have that, then, and only then should you be using save-post hook to update the meta data.

    Moderator bcworkz

    (@bcworkz)

    Assuming you do have a metabox sending data, and the field names are field1, field2, field3, then something like this will save the entered values:

    update_post_meta( $post_id, 'a', sanitize_text_field( $_POST['field1']));
    update_post_meta( $post_id, 'b', sanitize_text_field( $_POST['field2']));
    update_post_meta( $post_id, 'c', sanitize_text_field( $_POST['field3']));

    BTW, a lot of BBcode like [php] do not work here, next time you post code please use `backticks` or use the ‘code’ button above the edit box.

    Thread Starter lazercaveman

    (@lazercaveman)

    Okay Got it! 🙂

    …now i changed the code and startet constructing a plugin.

    Everything is working as it should. Posts are shown on search.php and so on… only when i klick on an article (wich is generated by my plugin) i get redirected to the startsite of my page :/

    So i searched for a solution, i read about i have to add a new single.php to my cstum-template which has to be named single-myposttypenam.php.

    So the posttype is registered as ements

    register_post_type(
            'ements',

    also i added a data which is named single-ements.php and added the code of the single.php into ist.

    But still, when i click on an article iIm getting redirected to my startpage :/

    What am i doing wrong??? :S

    thanks a lot for helping and Best regards 🙂

    Moderator bcworkz

    (@bcworkz)

    There’s something wrong with how links are generated on search.php. Please post the code within the loop section of that file, specifically the code that generates the output you are clicking on, and anything prior to that that is used to influence the output.

    Posting a link to the search results page with the bad links would also be helpful.

    Thread Starter lazercaveman

    (@lazercaveman)

    I allready found a solution… the permalink structure has to be setted to standard, than everything is working finally! 🙂

    THanks to everybody! 🙂

    Be sure to check off the box “Mark this topic as resolved” next to the “Post” button to close the topic.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Problem with ading customfields within functions.php’ is closed to new replies.