{"id":724,"date":"2020-04-06T15:50:00","date_gmt":"2020-04-06T13:50:03","guid":{"rendered":"https:\/\/wpfrontier.com\/?p=724"},"modified":"2020-04-24T12:12:51","modified_gmt":"2020-04-24T10:12:51","slug":"frontier-post-extension-plugin","status":"publish","type":"post","link":"https:\/\/wpfrontier.com\/frontier-post-extension-plugin\/","title":{"rendered":"Frontier Post &#8211; Extension Plugin ()"},"content":{"rendered":"<p>I have created a Demo plugin to show how Actions and Filters can be used in Frontier Post.<\/p>\n<p>Please be aware that this is for Demo purpose, and I do not help with Actions and Filters. To be able to utilise this you will need to have programming experience.<\/p>\n<p><strong>You can download the plugin here (zip file): <a href=\"https:\/\/wpfrontier.com\/wp-content\/uploads\/2020\/04\/frontier-post-my-extensions-v-1_1.zip\">frontier-post-my-extensions version 1.1<\/a><\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre class=\"language-php\"><code>\r\n\r\n\/\/***************************************************************************************\r\n\/\/* Add default content\r\n\/\/***************************************************************************************\r\n\r\nif (false)    \/\/Switch to false if you want to disable\r\n    {\r\n\r\n    function fp_my_template_content($tmp_post , $fpost_sc_parms)\r\n        {\r\n        \/\/ $fpost_sc_parms holds short code parameters\r\n        \r\n        \/\/ set post content & title\r\n        if ($tmp_post->post_content < \" \")\r\n            $tmp_post->post_content = 'Replace this with your template post content <br>and you are good to go';\r\n        \r\n        if ($tmp_post->post_title < \" \")\r\n            $tmp_post->post_title = 'Replace this with your template post title';\r\n        \r\n        \r\n        return $tmp_post;\r\n        } \r\n        \r\n        add_filter('frontier_post_prepare_post', 'fp_my_template_content',10,2);  \r\n    }\r\n    \r\n\/\/***************************************************************************************\r\n\/\/* Start output row to hold additional fields\r\n\/\/***************************************************************************************\r\n\r\n\/\/ New table ROW containing additional fields\r\n\/\/ Fields will be shown in bottom of form, if top instead use action frontier_post_form_standard_top (also for actions below)\r\nfunction fp_ext_start_row( ) \r\n    {\r\n    echo '<tr><table class=\"frontier_no_border\"><tr>';\r\n    }\r\nadd_action( 'frontier_post_form_standard', 'fp_ext_start_row', 10, 2 );\r\n    \r\n\/\/***************************************************************************************\r\n\/\/* Comment Status\r\n\/\/***************************************************************************************\r\n\r\nif (true)    \/\/Switch to false if you want to disable\r\n    {\r\n    function fp_ext_comment_status_field( $thispost, $tmp_task_new) \r\n        {\r\n\r\n        \/\/ Exit (so no field on form if not user is editor or admin)\r\n        if ( !current_user_can('edit_others_posts') )\r\n            return;\r\n    \r\n\r\n        \/\/ New table cell containing additional fields\r\n        echo '<td class=\"frontier_no_border\"  id=\"frontier_post_fieldset_cmt_status\">';\r\n        echo '<fieldset id=\"frontier_post_fieldset_cmt_status\" class=\"frontier_post_fieldset\">';\r\n        echo '<legend>'.__(\"Comment Status\",\"frontier-post\").'<\/legend>';\r\n         \r\n        \r\n        \/\/ Display Comment status field\t\t\r\n\t\t$open \t= ( (isset($thispost->comment_status) && $thispost->comment_status == \"open\") ? \"open\" : \"closed\"); \r\n\t\techo '<input class=\"fp_radioline\" type=\"radio\" id=\"fp_comment_status\" name=\"fp_comment_status\" value=\"open\" '.($open == 'open' ? 'checked=\"checked\"' : '').' >Open';\r\n\t\techo '<input class=\"fp_radioline\" type=\"radio\" id=\"fp_comment_status\" name=\"fp_comment_status\" value=\"closed\" '.($open == 'closed' ? 'checked=\"checked\"' : '').' >Closed';\r\n        \r\n        echo '<\/fieldset>';\r\n        echo '<\/td>';\r\n        }\r\n\r\n    \/\/ add the display action\t\r\n    add_action( 'frontier_post_form_standard', 'fp_ext_comment_status_field', 10, 2 );\r\n     \r\n    function fp_ext_save_comment_status($tmp_post, $tmp_task_new, $input_values )\r\n\t    {\r\n        \/\/ save Comment status\r\n        if ( array_key_exists('fp_comment_status', $input_values) )\r\n            $tmp_post['comment_status'] = $input_values['fp_comment_status'];\r\n\r\n        return $tmp_post;\r\n        } \/\/ end function Save comment status\r\n    \r\n      \r\n    add_filter('frontier_post_pre_update', 'fp_ext_save_comment_status',10,3);    \r\n    } \/\/ end if comment status\r\n\r\n    \r\n\r\n\/\/***************************************************************************************\r\n\/\/* Ping Status\r\n\/\/***************************************************************************************\r\n\r\nif (false) \/\/Switch to true if you want to enable\r\n   {\r\n    function fp_ext_ping_status_field( $thispost, $tmp_task_new) \r\n        {\r\n\r\n        \/\/ Exit (so no field on form if not user is editor or admin)\r\n        if ( !current_user_can('edit_others_posts') )\r\n            return;\r\n        \r\n        \/\/ New table cell containing additional fields\r\n        echo '<td class=\"frontier_no_border\" id=\"frontier_post_fieldset_ping\">';\r\n        echo '<fieldset id=\"frontier_post_fieldset_ping\" class=\"frontier_post_fieldset\">';\r\n        echo '<legend>'.__(\"Allow track- & pingbacks\",\"frontier-post\").'<\/legend>';\r\n        \r\n        \r\n        \/\/ Display Ping status field\t\t\r\n\t\t$open \t= ( (isset($thispost->ping_status) && $thispost->ping_status == \"open\") ? \"open\" : \"closed\"); \r\n\t\techo '<input class=\"fp_radioline\" type=\"radio\" id=\"fp_ping_status\" name=\"fp_ping_status\" value=\"open\" '.($open == 'open' ? 'checked=\"checked\"' : '').' >Open';\r\n\t\techo '<input class=\"fp_radioline\" type=\"radio\" id=\"fp_ping_status\" name=\"fp_ping_status\" value=\"closed\" '.($open == 'closed' ? 'checked=\"checked\"' : '').' >Closed';\r\n        \r\n        echo '<\/fieldset>';\r\n        echo '<\/td>';\r\n        }\r\n\r\n    \/\/ add the display action\t\r\n    add_action( 'frontier_post_form_standard', 'fp_ext_ping_status_field', 10, 2 );\r\n     \r\n    function fp_ext_save_ping_status($tmp_post, $tmp_task_new, $input_values )\r\n\t    {\r\n        \/\/ save ping status\r\n        if ( array_key_exists('fp_ping_status', $input_values) )\r\n            $tmp_post['ping_status'] = $input_values['fp_ping_status'];\r\n\r\n        return $tmp_post;\r\n        } \/\/ end function Save comment status\r\n    \r\n    add_filter('frontier_post_pre_update', 'fp_ext_save_ping_status',10,3);    \r\n\r\n    } \/\/ end if Ping status\r\n\r\n\r\n\/\/***************************************************************************************\r\n\/\/* Sticky post\r\n\/\/***************************************************************************************\r\n\r\nif (true) \/\/Switch to false if you want to disable\r\n    {\r\n    function fp_ext_sticky_field( $thispost, $tmp_task_new) \r\n        {\r\n        \r\n        \/\/ Exit (so no field on form if not user is editor or admin)\r\n        if ( !current_user_can('edit_others_posts') )\r\n            return;\r\n\r\n        \/\/ New table cell containing additional fields\r\n        echo '<td class=\"frontier_no_border\" id=\"frontier_post_fieldset_sticky\">';\r\n        echo '<fieldset id=\"frontier_post_fieldset_sticky\" class=\"frontier_post_fieldset\">';\r\n        echo '<legend>'.__(\"Sticky Post\",\"frontier-post\").'<\/legend>';\r\n        \r\n        \/\/check is post is sticky\r\n        $tmp_sticky = get_option('sticky_posts');\r\n        if (in_array($thispost->ID, $tmp_sticky))    \r\n            $sticky = \"Sticky\";\r\n        else \r\n            $sticky = \"Normal\";\r\n\r\n        \/\/ Display Sticky  field\t\t\r\n\t\techo '<input class=\"fp_radioline\" type=\"radio\" id=\"fp_sticky_status\" name=\"fp_sticky_status\" value=\"Sticky\" '.($sticky == 'Sticky' ? 'checked=\"checked\"' : '').' >Sticky';\r\n\t\techo '<input class=\"fp_radioline\" type=\"radio\" id=\"fp_sticky_status\" name=\"fp_sticky_status\" value=\"Normal\" '.($sticky == 'Normal' ? 'checked=\"checked\"' : '').' >Normal';\r\n        \r\n        echo '<\/fieldset>';\r\n        echo '<\/td>';\r\n        }\r\n       \r\n    \/\/ add the display action\t\r\n    add_action( 'frontier_post_form_standard', 'fp_ext_sticky_field', 10, 2 );\r\n     \r\n    function fp_ext_save_sticky_status($tmp_post, $tmp_task_new, $input_values )\r\n\t    {\r\n        \/\/ save Sticky status\r\n        $tmp_sticky     = get_option('sticky_posts');\r\n        $tmp_sticky_id  = 0;\r\n        if ( array_key_exists('fp_sticky_status', $input_values) && $input_values['fp_sticky_status'] == \"Sticky\")\r\n            {\r\n            \/\/ Add post id to sticky posts\r\n            if (!in_array($tmp_post->ID, $tmp_sticky)) \r\n                array_push($tmp_sticky, $tmp_post->ID);\r\n            }\r\n        else\r\n            {\r\n            \/\/ remove post id if exists in sticky posts\r\n            if (($key = array_search($tmp_post->ID, $tmp_sticky)) !== false) \r\n                {\r\n                unset($tmp_sticky[$key]);\r\n                }\r\n            }   \r\n            \r\n            update_option('sticky_posts',$tmp_sticky);\r\n        } \/\/ end function Save Stick post function\r\n\r\n    \/\/ Save the Sticky post status\r\n    \/\/ using frontier_post_post_save action as sticky post is not on post record, but the post id is stored in an option array.\r\n    add_action( 'frontier_post_post_save', 'fp_ext_save_sticky_status', 10, 3 );\r\n\r\n    } \/\/ end if Sticky\r\n\r\n\/\/***************************************************************************************\r\n\/\/* Author\r\n\/\/***************************************************************************************\r\n\r\nif (true) \/\/Switch to false if you want to disable\r\n    {\r\n    function fp_ext_author_field( $thispost, $tmp_task_new) \r\n        {\r\n\r\n        \/\/ Exit (so no field on form if not user is admin)\r\n        if ( !current_user_can('manage_options') )\r\n            return;  \r\n            \r\n        \/\/ New table cell containing additional fields\r\n        echo '<td class=\"frontier_no_border\" id=\"frontier_post_fieldset_author\">';\r\n        echo '<fieldset id=\"frontier_post_fieldset_author\" class=\"frontier_post_fieldset\">';\r\n        echo '<legend>'.__(\"Author\",\"frontier-post\").'<\/legend>';\r\n        \r\n        $qargs = array(\r\n            'selected'  => $thispost->post_author,\r\n            'name'      => 'fp_author_id',\r\n            'class'     => 'fp_change_author'\r\n                );\r\n        wp_dropdown_users( $qargs );\r\n\r\n        echo '<\/fieldset>';\r\n        echo '<\/td>';\r\n        }\r\n       \r\n    \/\/ add the display action\t\r\n    add_action( 'frontier_post_form_standard', 'fp_ext_author_field', 10, 2 );\r\n    \r\n    function fp_ext_save_author($tmp_post, $tmp_task_new, $input_values )\r\n\t    {\r\n        if ( array_key_exists('fp_author_id', $input_values) )\r\n            {\r\n            $tmp_author_id = absint($input_values['fp_author_id']);\r\n            if ($tmp_author_id > 0)\r\n                {\r\n                $tmp_post['post_author'] =  $tmp_author_id;\r\n                \/\/error_log(\"Saving author: \".$tmp_author_id );  \r\n                }\r\n            }\r\n        \r\n        \r\n        return $tmp_post;\r\n            \r\n        } \/\/ end function Save author\r\n    \r\n    \/\/ ** send $tmp_post back with author id    \r\n    \/\/ Using filter frontier_post_pre_update to ensure that save goes through the normal validation, instead of update Auther after post is saved.\r\n    add_filter('frontier_post_pre_update', 'fp_ext_save_author',10,3);  \r\n\r\n    } \/\/ end if Author\r\n\r\n\r\n\r\n\/\/***************************************************************************************\r\n\/\/* Future posts - Show post date field, and save \r\n\/\/***************************************************************************************\r\n\r\nif (true) \/\/Switch to false if you want to disable\r\n   {\r\n\r\n    \/\/ Exit (so no field on form if not user is editor or admin)\r\n    \/\/if ( !current_user_can('edit_others_posts') )\r\n    \/\/return;\r\n\r\n\r\n    \/\/ ***********************************\r\n    \/\/ ** load jquery datepicker        **\r\n    \/\/ ***********************************\r\n    \r\n    function fp_ext_my_datepick()\r\n        {\r\n        wp_enqueue_script( 'jquery-ui-core' );\r\n        wp_enqueue_script('jquery-ui-datepicker');\r\n        wp_enqueue_style('e2b-admin-ui-css','https:\/\/ajax.googleapis.com\/ajax\/libs\/jqueryui\/1.9.0\/themes\/base\/jquery-ui.css',false,\"1.9.0\",false);\r\n        }\r\n    add_action('wp_enqueue_scripts', 'fp_ext_my_datepick');\r\n    \r\n\r\n    \/\/ ***********************************\r\n    \/\/ ** Add fields to  post form      **\r\n    \/\/ ***********************************\r\n    \r\n    function fp_ext_post_date_field( $thispost, $tmp_task_new) \r\n        {\r\n        \/\/ js script to set class for datepicker\r\n        echo '<script>\r\n            jQuery(function() {\r\n            jQuery( \".datepicker\" ).datepicker({\r\n                dateFormat : \"yy-mm-dd\"\r\n            });\r\n            });\r\n            <\/script>';\r\n\r\n        \/\/ New table cell containing additional fields\r\n        echo '<td id=\"frontier_post_fieldset_post_date\" class=\"frontier_no_border\">';\r\n        echo '<fieldset id=\"frontier_post_fieldset_post_date\" class=\"frontier_post_fieldset\">';\r\n        echo '<legend>'.__('Publish Time', 'frontiier-post').'<\/legend>';\r\n        \r\n                   \r\n        \r\n        \/\/ Post date field\r\n        $tmp_post_date\t= ($thispost->post_date ? $thispost->post_date : date(\"Y-m-d h:i\"));\r\n        $tmp_date\t\t= substr($tmp_post_date,0,10);\r\n        $tmp_hour\t\t= substr($tmp_post_date,11,2);\r\n        $tmp_minute\t\t= substr($tmp_post_date,14,2);\r\n            \r\n        echo '<input name=\"fp_post_date\" id=\"fp-post-date\" value=\"'.$tmp_date.'\" type=\"text\" class=\"datepicker fp-post-date\"\/>';\r\n        echo '&nbsp;&nbsp;&nbsp;';\r\n        echo '<input name=\"fp_post_hour\" id=\"fp_post_hour\" value=\"'.$tmp_hour.'\" type=\"text\" class=\"fp-post-hour\"\/>';\r\n        echo ':';\r\n        echo '<input name=\"fp_post_minute\" id=\"fp_post_minute\" value=\"'.$tmp_minute.'\" type=\"text\" class=\"fp-post-minute\"\/>';\t\r\n    \r\n        echo '<\/fieldset>';\r\n        echo '<\/td>';\r\n        }\r\n        \r\n    \/\/ add the display action\t\r\n    add_action( 'frontier_post_form_standard', 'fp_ext_post_date_field', 10, 2 );\r\n    \r\n    \/\/ add save post date\r\n\tfunction fp_ext_save_post_date($tmp_post, $tmp_task_new, $input_values )\r\n        {\r\n        \/\/ Check if post date field is in the imput form    \r\n        if ( array_key_exists('fp_post_date', $input_values) )\r\n            {\r\n            $tmp_post_date \t\t= $input_values['fp_post_date'];\r\n            $tmp_post_hour \t\t= zeroise(intval($input_values['fp_post_hour']),2);\r\n            $tmp_post_minute \t= zeroise(intval($input_values['fp_post_minute']),2);\r\n            \r\n            $tmp_year \t= substr($tmp_post_date ,0,4);\r\n            $tmp_month \t= substr($tmp_post_date ,5,2);\r\n            $tmp_day \t= substr($tmp_post_date ,8,2);\r\n            \r\n            $tmp_publish_time\t= $tmp_post_date.\" \".$tmp_post_hour.\":\".$tmp_post_minute;\r\n            \r\n            \/\/ Check if valid date time\r\n            if ( !checkdate($tmp_month, $tmp_day, $tmp_year) || $tmp_post_hour>23 || $tmp_post_minute>59 )\r\n                {\r\n                frontier_post_set_msg('<div id=\"frontier-post-alert\">post_date: Error Invalid date or time: '.$tmp_publish_time.'<\/div>');\r\n                }\r\n            else\r\n                {\r\n                $tmp_post['post_date'] = $tmp_publish_time;\r\n                \r\n                \/\/ set status to future, if post_date is in the future\r\n                if ( strtotime(get_gmt_from_date($tmp_publish_time)) > strtotime('now') && in_array($tmp_post['post_status'], array('future', 'publish')) )\r\n                    {\r\n                    \r\n                    $tmp_post['post_status'] \t= 'future';\r\n                    \/\/ edit_date & post_date_gmt is neccssary for future post scheduling to work, not logical, but WordPress :)\r\n                    $tmp_post['edit_date'] \t\t= true;\r\n                    $tmp_post['post_date_gmt'] \t= get_gmt_from_date( $tmp_post['post_date'] );\r\n                    \r\n                    }\r\n                else\r\n                    {\r\n                    \/\/If post status is Futue, but time is in the past we better publish the post\r\n                    if ($tmp_post['post_status'] === 'future' )\r\n                        {\r\n                        $tmp_post['edit_date'] \t\t= true;\r\n                        $tmp_post['post_status'] \t= 'publish';\r\n                        }\r\n                    }\r\n                } \/\/ end validate date \/ time\r\n            } \/\/ end check date\r\n            return $tmp_post;\r\n        } \/\/ end function Save post date\r\n        \r\n        add_filter('frontier_post_pre_update', 'fp_ext_save_post_date',10,3);\r\n    \r\n\r\n    } \/\/ *** End display frontier Post Date Field\r\n\r\n\r\n\r\n\/\/***************************************************************************************\r\n\/\/* Rating (Custom field)\r\n\/\/***************************************************************************************\r\n\r\nif (true) \/\/Switch to false if you want to disable\r\n    {\r\n    function fp_ext_rating_field( $thispost, $tmp_task_new) \r\n        {\r\n        \r\n        \/\/ Exit (so no field on form if not user is editor or admin)\r\n        if ( !current_user_can('manage_options') )\r\n            return;\r\n\r\n        \/\/ New table cell containing additional fields\r\n        echo '<td class=\"frontier_no_border\" id=\"frontier_post_fieldset_rating\">';\r\n        echo '<fieldset id=\"frontier_post_fieldset_rating\" class=\"frontier_post_fieldset\">';\r\n        echo '<legend>'.__(\"Rating\",\"frontier-post\").'<\/legend>';\r\n        \r\n        \/\/ Get value from Custom Field fp_rating\t\t\r\n\t\t$rating = intval(get_post_meta( $thispost->ID, 'fp_rating', true ));\r\n        \r\n        $tmp_html = '<select class=\"fp_change_rating\" name=\"fp_rating\" id=\"fp_rating\" >';\r\n        $tmp_html .= '<option  value=\"0\"'.($rating == 0 ? ' selected=\"selected\" >' : '>').'Not rated<\/option>';\r\n        \/\/ Build from 1 to 5 star rating\r\n        for ($i = 1; $i <= 5; $i++)\r\n            {\r\n            $tmp_html .= '<option  value=\"'.$i.'\"'.($rating == $i ? ' selected=\"selected\" >' : '>').$i.' Stars<\/option>';\r\n            }\r\n    \r\n        $tmp_html = $tmp_html.'<\/select>';\r\n        echo $tmp_html;\r\n    \r\n\r\n        echo '<\/fieldset>';\r\n        echo '<\/td>';\r\n        }\r\n       \r\n    \/\/ add the display action\t\r\n    add_action( 'frontier_post_form_standard', 'fp_ext_rating_field', 10, 2 );\r\n     \r\n    function fp_ext_save_rating($tmp_post, $tmp_task_new, $input_values )\r\n\t    {\r\n        \/\/ save Rating\r\n        \r\n        if ( array_key_exists('fp_rating', $input_values) )\r\n            {\r\n            update_post_meta($tmp_post->ID, 'fp_rating', intval($input_values['fp_rating']) );\r\n            }\r\n            \r\n        } \/\/ end Save rating function\r\n\r\n    \/\/ Save the rating\r\n    \/\/ using frontier_post_post_save action as Rating is not on post record, but the post id is stored as a custom Field.\r\n    add_action( 'frontier_post_post_save', 'fp_ext_save_rating', 10, 3 );\r\n\r\n    } \/\/ end if Sticky\r\n\r\n\r\n\r\n\r\n\/\/***************************************************************************************\r\n\/\/* Close output row to hold additiuonal fields\r\n\/\/***************************************************************************************\r\n\r\n\/\/ Close table ROW containing additional fields\r\nfunction fp_ext_close_row( ) \r\n    {\r\n    echo '<tr><\/table><\/tr>';\r\n    }\r\nadd_action( 'frontier_post_form_standard', 'fp_ext_close_row', 10, 2 );\r\n    \r\n\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have created a Demo plugin to show how Actions and Filters can be used in Frontier Post. Please be aware that this is for Demo purpose, and I do not help with Actions and Filters. To be able to <span class=\"excerpt-dots\">&hellip;<\/span> <a class=\"more-link\" href=\"https:\/\/wpfrontier.com\/frontier-post-extension-plugin\/\">[CLICK TO READ MORE]<\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-724","post","type-post","status-publish","format-standard","hentry","category-frontier-post","article-type-code-sniplet","article-type-demo","topics-hooks-actions-filters"],"_links":{"self":[{"href":"https:\/\/wpfrontier.com\/wp-json\/wp\/v2\/posts\/724","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpfrontier.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpfrontier.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpfrontier.com\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/wpfrontier.com\/wp-json\/wp\/v2\/comments?post=724"}],"version-history":[{"count":12,"href":"https:\/\/wpfrontier.com\/wp-json\/wp\/v2\/posts\/724\/revisions"}],"predecessor-version":[{"id":772,"href":"https:\/\/wpfrontier.com\/wp-json\/wp\/v2\/posts\/724\/revisions\/772"}],"wp:attachment":[{"href":"https:\/\/wpfrontier.com\/wp-json\/wp\/v2\/media?parent=724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpfrontier.com\/wp-json\/wp\/v2\/categories?post=724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpfrontier.com\/wp-json\/wp\/v2\/tags?post=724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}