Skip to content

Instantly share code, notes, and snippets.

View rahularyan's full-sized avatar
🏠
Working from home

Rahul Arya rahularyan

🏠
Working from home
View GitHub Profile
@rahularyan
rahularyan / custom-field.php
Last active December 3, 2017 11:01
Add a PDF upload field in AnsPress questions
<?php
/**
* Adds a PDF field to AnsPress question form.
*
* @param array $form Form arguments.
* @return void
*/
function my_custom_question_field( $form ) {
<?php
/**
* Filter select field.
*
* Check if select field options are terms and then change orderby and order value.
* This filter will work only with AnsPress 4.1.0
*/
function my_category_field_filter( $args, $form ) {
if ( 'select' === $args['type'] && 'terms' === $args['options'] ) {
$args['terms_args']['orderby'] = 'orderby';
<?php
// AnsPress upload field.
array(
'label' => __( 'Prove that you are a human', 'anspress-question-answer' ),
'type' => 'captcha',
'order' => 100, // Make sure to keep it at bottom
);
<?php
/**
* Save custom form "sample_form".
*
* @return void
*/
function my_custom_ap_form() {
$form = anspress()->get_form( 'sample_form' );
// Get all sanitized values.
<?php
// AnsPress form tags field example.
array(
'label' => __( 'Tags', 'anspress-question-answer' ),
'desc' => sprintf(
// Translators: %1$d contain minimum tags required and %2$d contain maximum tags allowed.
__( 'Tagging will helps others to easily find your question. Minimum %1$d and maximum %2$d tags.', 'anspress-question-answer' ),
ap_opt( 'min_tags' ),
ap_opt( 'max_tags' )
),
<?php
// AnsPress upload field.
array(
'html' => '<div class=""><h3>A simple header</h3></div>',
);
<?php
// AnsPress repeatable field.
array(
...
'type' => 'repeatable',
'fields' => array(
'image-title' => array(
'label' => __( 'Image title' ),
'type' => 'input',
),
<?php
// AnsPress group field.
array(
...
'type' => 'group',
'toggleable' => true, // Checkbox for showing and hiding group.
'fields' => array(
'child-1' => array(
'label' => __( 'A simple child text field' ),
'type' => 'input',
<?php
// AnsPress upload field.
array(
...
'type' => 'upload',
'upload_options' => array(
'allowed_mimes' => ap_allowed_mimes(), // Or you can allow your own types.
'max_files' => 1, // Numbers of files allowed to upload.
'multiple' => false, // This must be true if you wish to upload multiple files.
'label_deny_type' => __( 'This file type is not allowed to upload.', 'anspress-question-answer' ),
<?php
// AnsPress upload field.
array(
...
'type' => 'editor',
'editor_args' => array(
// Pass tinyMce options.
),
'upload_options' => array(
// Pass upload field options.