Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

Adding custom dns-entries on sub-domain creation

Discussion in 'cPanel Developers' started by mhofer, May 19, 2013.

  1. mhofer

    mhofer Member

    May 10, 2013
    Likes Received:
    Trophy Points:
    cPanel Access Level:
    Root Administrator

    Because we were not satisfied with the default proxy-subdomain creation of cpanel/whm, we try to create our own.
    For domains / parked domains / addon-domains this was no problem by editing the Zone-Template files.

    Unfortunately I could not find something similar for subdomains... that's why i'm trying to create something manually.

    I tried it with the as follows:
    package Cpanel::CustomEventHandler;
    # cpanel12 -                      Copyright(c) 2012 cPanel, Inc.
    #                                                           All rights Reserved.
    # This code is subject to the cPanel license. Unauthorized copying is prohibited
    use strict;
    use Cpanel::Logger ();
    # api_version = api_version
    # type = pre,post
    # module = Cpanel::<modname>
    # event = the event ie addpop
    # cfg ref is a hash of the conf variables passed to the event. If its a legacy event, the hash keys are numbered, newer events have names.
    # dataref = the data returned from the event (post events only)
    sub event {
        my ( $api_version, $type, $module, $func, $cfgref, $dataref ) = @_;
        ## UAPI note: $api_version will be '3', and the method signature becomes the similar:
        ##   my ( $api_version, $type, $module, $func, $args, $result ) = @_;
        if ($module eq 'subdomain') {
            if ($func eq 'addsubdomain') {
                if ($type eq 'post') {
                    my $hdata = $cfgref->[0];
                    my $sub = $hdata->{'domain'};
                    my $domain = $hdata->{'rootdomain'};
                    #require Cpanel::API
                    Cpanel::API::execute( 'ZoneEdit', 'add_zone_record', { domain => $domain, name => 'autoconfig.' .  $sub, type => 'CNAME', cname => $domain . '.' } );
        return 1;
    when i create a subdomain in cpanel, it gets created normally and this script has no effect.

    Can someone help me figure out why? Is there an easier solution to accomplish this?

    Following should be done:
    - user creates subdomain
    - system creates several custom dns-entries based on this subdomain
  2. KostonConsulting

    KostonConsulting Well-Known Member

    Jun 17, 2010
    Likes Received:
    Trophy Points:
    San Francisco, CA
    cPanel Access Level:
    Root Administrator
    Don't use, it's an old system and there can only be one so you run the risk of it getting overwritten by some 3rd party plugin that hasn't moved past that hook yet.

    Instead, use cPanel's Standardized Hooks (Introduction to Standardized Hooks). Here's an example of adding a hook for Subdomain::addsubdomain():

    Hook code:

    package ScriptHook;
    sub describe {
        my $hooks = {
            'hook'      => 'ScriptHook::changedns',
            'function'  => 'Api2::Subdomain::addsubdomain',
            'exectype'  => 'module',
            'namespace' => 'Cpanel',
            'stage'     => 'post'
        return $hooks;
    sub changedns {
        #require Cpanel::API
        Cpanel::API::execute( 'ZoneEdit', 'add_zone_record', { domain => $domain, name => 'autoconfig.' .  $sub, type => 'CNAME', cname => $domain . '.' } );

    Here's how to add the module hook:

    $mkdir /var/cpanel/perl5
    $mkdir /var/cpanel/perl5/lib
    $cp /var/cpanel/perl5/lib/
    $/usr/local/cpanel/bin/manage_hooks add module ScriptHook
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice